22

I have an array like Array

(
    [0] => A
    [2] => B
    [4] => C
    [6] => D
)

I want to remove the first element and then re-index array to get the output

(
    [0] => B
    [1] => C
    [2] => D
)

Which PHP function i need to use?


Update

Input array is

Array
(
    [0] => Array
        (
            [0] => Some Unwanted text
            [1] => You crazyy
        )

    [2] => Array
        (
            [0] => My belowed text
            [1] => You crazyy
        )

    [10] => Array
        (
            [0] => My loved quote
            [1] => You crazyy
        )

)

And the output should be like

Array
(
    [0] => Array
        (
            [0] => My belowed text
            [1] => You crazyy
        )

    [1] => Array
        (
            [0] => My loved quote
            [1] => You crazyy
        )

)
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
Mithun Sreedharan
  • 49,883
  • 70
  • 181
  • 236

5 Answers5

36

You can use

array_shift($array)

Documentation for array_shift

Alexander Nied
  • 12,804
  • 4
  • 25
  • 45
User123
  • 566
  • 5
  • 16
28

With array_splice.

http://www.php.net/manual/en/function.array-splice.php

php > print_r($input);
Array
(
    [0] => A
    [2] => B
    [4] => C
    [6] => D
)
php > array_splice($input, 0, 1);
php > print_r($input);
Array
(
    [0] => B
    [1] => C
    [2] => D
)

esamatti
  • 18,293
  • 11
  • 75
  • 82
  • 1
    Need to omit the third parameter array_splice($input, 0) – Mithun Sreedharan Jun 09 '10 at 05:58
  • 3
    Use array_splice($input, 1); // 1 is start index to get this array ([0] => B, [1] =>C, [2] => D) – sayvortana May 01 '13 at 11:05
  • This is perfect if you want the actual array returned from the function, which is what I needed. Rather than using `array_shift` which returns the removed element for some reason known only to the PHP Devs?!? – superphonic Apr 04 '17 at 16:36
3

we can do it with array_shift() which will remove the 1st index of array and after that use array_values() which will re-index the array values as i did not get from the @User123's answer, try below one:

<?php
    $array = array(
                0 => "A",
                2 => "B",
                4 => "C",
                6 => "D"
            );
    array_shift($array);
    $array = array_values($array);
    echo "<pre>";
    print_r($array);

Output: check the output here https://eval.in/837709

Array
    (
        [0] => B
        [1] => C
        [2] => D
    )

Same for your Updated Input array

<?php
    $array = array(
                    0 => array(
                            0 => "Some Unwanted text",
                            1 => "You crazyy"
                        ),

                    2 => array(
                            0 => "My belowed text",
                            1 => "You crazyy"
                        ),

                    10 => array(
                            0 => "My loved quote",
                            1 => "You crazyy"
                        )

                );
    array_shift($array);
    $array = array_values($array);
    echo "<pre>";
    print_r($array);

Output: check the output here https://eval.in/837711

Array
(
    [0] => Array
        (
            [0] => My belowed text
            [1] => You crazyy
        )

    [1] => Array
        (
            [0] => My loved quote
            [1] => You crazyy
        )

)
lazyCoder
  • 2,544
  • 3
  • 22
  • 41
2

You can cut the array as many many index as you want

$newArray = array_splice($oldArray, $startIndex, $lengthToSlice);
Mahbub Alam
  • 368
  • 2
  • 6
0
$array=array(
0 => 'A',
2 => 'B',
4 => 'C',
6 => 'D'
);

unset($array[0]);
$array = array_values($array);
print_r($array);

This is also another solution to this issue using unset

Output:

Array
(
    [0] => B
    [1] => C
    [2] => D
)
pr1nc3
  • 8,108
  • 3
  • 23
  • 36