php

PHP array_shift Function

array_shift() in PHP is used to return the first element by removing it from the given input array. Display the array after performing the array_shift() function if we want to return the remaining array.

Syntax:
array_shift(array_input)

Parameter:
array_input is the array

Let’s see examples to understand this concept better.

Example 1:

In this example, we will create an array with four key-values: ‘flower1’=>’lotus’,’flower2’=>’lilly’,’flower3’=>’jasmine’,’flower4’=>’rose’.

Let’s remove the first pair using the array_shift() function.

<?php

//create an array named Flower with 4 key-values.
$Flower=array('flower1'=>'lotus','flower2'=>'lilly','flower3'=>'jasmine','flower4'=>'rose');

echo 'Actual Array: ';
print_r($Flower);

//remove the first element from Flower
echo 'Element removed from array is:' .array_shift($Flower).'\n';

echo 'Final Array: ';
print_r($Flower);

?>

Output:

Here, the first element present in the array is the lotus. So, it is removed and returns the remaining array.

Example 2:

In this example, we will create an array with four values: ‘lotus’,’lilly’,’jasmine’,’rose’.

Let’s remove the first element using the array_shift() function.

<?php

//create an array named Flower with 4 values.
$Flower=array('lotus','lilly','jasmine','rose');

echo "Actual Array: ";
print_r($Flower);

//remove the first element from Flower
echo "Element removed from array is:" .array_shift($Flower)."\n";

echo "Final Array: ";
print_r($Flower);

?>

Output:

We can see that the first element present in the array is the lotus. So, it is removed and returns the remaining array.

Example 3:

It is also possible to remove the first nested array inside the array. In this example, we will create a nested array that has 2 arrays:

$flowers=array('lotus','lilly','jasmine','rose')

and

$veg=array('carrot','onion','chillies')

Now, we will remove $flowers from the array using array_shift() function.

<?php

//create 2 arrays in an array
$Flower=array($flowers=array('lotus','lilly','jasmine','rose'), $veg=array('carrot','onion','chillies'));

echo 'Actual Array: ';
print_r($Flower);

//remove the first array from Flower
echo 'Element removed from array is:' .'\n';
print_r(array_shift($Flower));

echo 'Final Array: ';
print_r($Flower);

?>

Output:

We can see that the first nested array is removed and returned as a $veg nested array.

Example 4:

In this example, inside an array, the first array is a nested array that has one array with one element(lotus) and the second array has one element(carrot). So, after applying array_shift(), the lotus is removed and the carrot array is returned.

<?php

//create 2 arrays in an array
$Flower=array($flowers=array(array('lotus')),$veg=array('carrot'));

echo "Actual Array: ";
print_r($Flower);

//remove the first array from Flower
echo "Element removed from array is:" ."\n";
print_r(array_shift($Flower));

echo "Final Array: ";
print_r($Flower);

?>

Output:

Conclusion

We discussed the array_shift() function to remove the first element by considering four different examples. It deletes and returns the first element from an array by taking an input array as a parameter.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain