PHP array_shift() Function
In this chapter you will learn:
- Definition for PHP array_shift() Function
- Syntax for PHP array_shift() Function
- Parameter for PHP array_shift() Function
- Example - Returns the value from the front of the array and remove it from the array
- Example - Remove elements from an associate array
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/* j a va 2 s . c o 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 j a v a 2 s.co m
$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.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP array_slice() Function
- Syntax for PHP array_slice() Function
- Parameter for PHP array_slice() Function
- Example - slice from 2
- Example - Start the slice from from the second array element, and return only two elements
- Example - Using a negative start parameter
- Example - With the preserve parameter set to true
- Example - With both string and integer keys
Home » PHP Tutorial » PHP Array Functions