Remove First Item From Array In Php

PHP

Nicesnippets

437

13-07-2021


Hi Guys,

In this example,I will learn you how to remove first element from array in php.you can easy and simply remove first element from array in php.

You can use the PHP array_shift() function to remove the first element or value from an array. The array_shift() function also returns the removed value of array.

Example 1:


<?php

$hobby = ["Cricket", "Football", "Badminton"];

array_shift($hobby);

print_r($hobby);

?>

Output:

Array

(

[0] => Football

[1] => Badminton

)

Example 2:

<?php

$friends = [

[ "id" => 1, "name" => "Mehul"],

[ "id" => 2, "name" => "Dharmik"],

[ "id" => 3, "name" => "Keval"],

];

array_shift($friends);

print_r($friends);

?>

Output:

Array

(

[0] => Array

(

[id] => 2

[name] => Dharmik

)

[1] => Array

(

[id] => 3

[name] => Keval

)

)

It will help you...