How to pop the first element of an array in PHP

1 Answer

0 votes
$array = [1, 2, 3, 4, 5, 6];

// Pop the first element
$firstElement = array_shift($array);

echo "Popped element: " . $firstElement . "\n";

print_r($array);



/*
run:

Popped element: 1
Array
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
    [4] => 6
)

*/

 



answered Apr 27, 2025 by avibootz
...