array_shift(): Taking out first element of the array
$output=array_shift($input);
Take out the first element of the array and returns the same. After applying array_shift() total elements of the array reduces by one. All numerical array keys are re-indexed but literal keys remain same.
Returns the shifted element ( first element ) of the array.
Here $input is an array, $output get the value of first element of the array.
<?Php
$input=array('One','Two','Three','Four','Five');
$output=array_shift($input);
echo $output; // Output is One
echo "<br><br>";
while (list ($key, $val) = each ($input)) {
echo "$key -> $val <br>";
}
?>
Output of above code is here( $output gets the first element One )
One
0 -> Two
1 -> Three
2 -> Four
3 -> Five
With Literal Keys
$input=array('Fruits1' =>'Banana','Fruits2'=>'Mango','Fruits3'=>'Apple','Fruits4'=>'Grapes');
$output=array_shift($input);
echo $output; // Output is Banana
echo "<br><br>";
while (list ($key, $val) = each ($input)) {
echo "$key -> $val <br>";
}
Output is here, ( $output gets the first element Banana)
Banana
Fruits2 -> Mango
Fruits3 -> Apple
Fruits4 -> Grapes