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//  ww  w  .j  a  v a2  s  .co  m
$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/*from   w ww .  j  a va 2 s  . com*/
$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.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions