Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove the first element from a multidimensional array with PHP

Tags:

arrays

php

Form this array, how can I remove the first element of Provinces ?

Array
(
    [Country] => Canada
    [Provinces] => Array
        (
            [0] => Quebec
            [1] => Ontario
            [2] => British Columbia
        )
)

Thanks.

like image 637
Thomas Avatar asked Mar 06 '26 04:03

Thomas


1 Answers

If you want to remove the first items in the array for the key name Provinces and the numerical keys do not have to be preserved, you could also use array_splice:

$arr = [
    "Country" => "Canada",
    "Provinces" => [
        "Quebec",
        "Ontario",
        "British Columbia"
    ]
];
array_splice($arr["Provinces"], 0, 1);

Php demo

Or using unset to keep the numerical keys:

unset($arr['Provinces'][0]);
like image 106
The fourth bird Avatar answered Mar 07 '26 17:03

The fourth bird



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!