array_shift


php128 apg

SHIFTS an element off the beginning of a given ARRAY.





This function shifts the first value of the given $array off and returns it, shortening the $array by one element and moving everything down.

All literal keys won't be affected.

Numerical array keys will be modified to start counting from zero.

This function returns FALSE if the $array is EMPTY.

You may use the  ===  operator for testing the return value of this function.



<?php

mix array_shift 
arr &$array )

where,

$array The given input ARRAY

?>

 $array 


The given input array.

Can be passed by reference.



  1 EXERCISE   

<?php

$arr01 
= [ "K" => 2"L" => 8
                
"M" => 18"N" => 32
                
"O" => 32"P" => 18"Q" => ];
                  
echo 
'The given ARRAY:<br>';
print_r($arr01);

array_shift($arr01);
echo 
'<br><br>After a first shift:<br>';
print_r($arr01);

array_shift($arr01);
echo 
'<br><br>After a second shift:<br>';
print_r($arr01);

array_shift($arr01);
echo 
'<br><br>After a third shift:<br>';
print_r($arr01);

array_shift($arr01);
echo 
'<br><br>After a forth shift:<br>';
print_r($arr01);

array_shift($arr01);
echo 
'<br><br>After a fifth shift:<br>';
print_r($arr01);

?>

 RESULT   

The given ARRAY:
Array
(
[K] => 2
[L] => 8
[M] => 18
[N] => 32
[O] => 32
[P] => 18
[Q] => 8
)


After a first shift:
Array
(
[L] => 8
[M] => 18
[N] => 32
[O] => 32
[P] => 18
[Q] => 8
)


After a second shift:
Array
(
[M] => 18
[N] => 32
[O] => 32
[P] => 18
[Q] => 8
)


After a third shift:
Array
(
[N] => 32
[O] => 32
[P] => 18
[Q] => 8
)


After a forth shift:
Array
(
[O] => 32
[P] => 18
[Q] => 8
)


After a fifth shift:
Array
(
[P] => 18
[Q] => 8
)


  2 EXERCISE   

<?php

$var02 
= [ => [0,1], => [0,2], => "three" ]; 

echo 
'The given ARRAY:<br>';
print_r($var02);

if(
array_shift($var02))
{
echo 
'<br><br>After only one shift:<br>';
print_r($var02);
}
else
{
echo 
'The shift is IMPOSSIBLE!';


?>

 RESULT   

The given ARRAY:
Array
(
[1] => Array
(
[0] => 0
[1] => 1
)

[2] => Array
(
[0] => 0
[1] => 2
)

[3] => three
)


After only one shift:
Array
(
[0] => Array
(
[0] => 0
[1] => 2
)

[1] => three
)


  3 EXERCISE   

<?php

// EMPTY ARRAY
$var03 = array();

if(
array_shift($var03))
{
print_r($var03);
}
else
{
echo 
'The shift is IMPOSSIBLE!';
}

?>

 RESULT   

The shift is IMPOSSIBLE!

  4 EXERCISE   

<?php

$var04 
= [ ["yesterday""today""tomorrow"], "When?" ];

echo 
'The given ARRAY:<br>';
print_r($var04);

if(
array_shift($var04))
{
echo 
'<br><br>After only one shift:<br>';
print_r($var04);
}
else
{
echo 
'The shift is IMPOSSIBLE!';
}  

?>

 RESULT   

The given ARRAY:
Array
(
[0] => Array (
[0] => yesterday
[1] => today
[2] => tomorrow
)

[1] => When?
)


After only one shift:
Array
(
[0] => When?
)