You can use the PHP array_shift() function remove the first element from an array in PHP. The array_shift() function returns the first value of the array. If array is empty or is not an array then NULL will be returned.
You can use the PHP reset and key functions remove the First Element of Associative from an array in PHP.
reset(), set the internal pointer of an array to its first element.
key() fetch a key from an array.
First let’s see the $array output :
1 2 3 4 5 6 7 8 |
<?php $array = ["name"=>"Tom", "lastname"=>"Violet","age"=>40]; print_r($array); ?> |
Output:
1 2 3 4 5 6 7 8 |
Array ( [name] => Tom [lastname] => Violet [age] => 40 ) |
Now we will use PHP reset() and key() functions to Get First Element of an Associative array like in below example
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $array = ["name"=>"Tom", "lastname"=>"Violet","age"=>40]; reset($array); $first_key = key($array); echo $first_key ."<br>"; echo $array[$first_key] ."<br>"; ?> |
Output:
1 2 3 4 |
name Tom |