PHP Tutorial - PHP array_shift() Function






Definition

The array_shift() function returns the value from the front of the array and remove it from the array.

Syntax

PHP array_shift() Function has the following syntax.

mixed array_shift ( array &arr )

Parameter

  • arr - Required. Array to shift

Example 1

Returns the value from the front of the array and remove it from the array


<?PHP
$names = array("A", "B", "C", "D", "E", "java2s.com");
$firstname = array_shift($names);
print($firstname);
var_dump($names);
?>

The code above generates the following result.





Example 2

array_shift() removes the first element from an array, and returns its value (but not its key).

To use it, pass the array in question to array_shift() :


<?PHP
$myBook = array( "title" =>  "Learn PHP from java2s.com", 
                 "author" =>  "java2s.com", 
                 "pubYear" =>  2000 ); 

echo array_shift( $myBook ) . "\n";
print_r( $myBook );   
?>

The code above generates the following result.