remove first array from an array

Asked

Viewed 224 times

0

I have the following array and would like to always remove the first array, how can I do that? Obs: there is the possibility that the first array may not always have index 0.

Array
    (
        [0] => Array
            (
                [0] => number
            )

        [1] => Array
            (
                [0] => 101010100
            )
        [2] => Array
            (
                [0] => 30303030
            )
)

3 answers

3

Use the array_shift:

$arr  = [['number'], [101010100], [30303030]];

array_shift($arr);

print_r($arr);

Will return:

Array
(
    [0] => Array
        (
            [0] => 101010100
        )

    [1] => Array
        (
            [0] => 30303030
        )

)

Example in ideone

  • I tried with this function before asking the question, but it is only leaving the first array and removes the rest

  • What version of PHP are you using? See ideone the working example.

2

    <?php
            $cars = array
                 (
                 array("Volvo",22,18),
                 array("BMW",15,13),
                 array("Saab",5,2),
                 array("Land Rover",17,15)
                 );
            array_shift($cars); //Retira o primeiro elemento do array
    ?>
  • I tried with this function before asking the question, but it is only leaving the first array and removes the rest

2

You can use the array_splice function and remove a portion of the array.

$novoArray = array_splice($array, 1);

Remove to first position and adjust the contents.

  • Thank you, it worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.