PHP array_shift() Function Example

PHP Script:


<?php
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_shift($stack);
print_r($fruit);
echo "<br />";
print_r($stack);
?>

Output:


orange
Array
(
[0] => banana
[1] => apple
[2] => raspberry
)

Description:

PHP array_shift() function shifts off/pops the first/top value of the array and returns it, reducing the array by one element and moving everything down. The numeric key will be re-assigned starting from zero, while the literal keys would remain unchanged.
This method resets the array pointer of the input array after use.

PHP array_shift function parameters:

1- array: The array to be shifted.

PHP array_shift function output:

1- mixed: Returns the shifted value, or NULL if array is empty or it is not an array, where shifted value can itself be an array.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s