MENU
Iteration and Selection
array_push(&$arr,$m1[,$m2……]) adds $m1…… onto the the end of $arr. It returns the new number of elements in $arr. array_pop(&$arr) removes and returns the last value of $arr. The array pointer will be reset. array_unshift(&$arr, $m1 [,$m2……]) adds $m1……onto the front of $arr. It returns the new number of elements in $arr. All numerical keys will be modified to start counting from zero. array_shift(&$arr) removes and returns the first value of $arr. The array pointer will be reset. All numerical keys will be modified to start counting from zero. array_rand($arr[,$i=1]) picks $i random keys from $arr. If more than one entries are picked, an array will be returned.<!DOCTYPE html><html><head></head>
<body>
<?php
$a=['a','b','c','d','e','f'];
array_pop($a);
array_push($a,'X','Y');
array_shift($a);
array_unshift($a,'M','N');
print_r($a); echo "<br />";
$r=array_rand($a,3);
echo $a[$r[0]]." ".$a[$r[1]]." ".$a[$r[2]];
?>
</body></html>Array ( [0] => M [1] => N [2] => b [3] => c [4] => d [5] => e [6] => X [7] => Y )
M c d
<!DOCTYPE html><html><head></head>
<body>
<?php
$a=['a','b','c','test'=>'d','e','f'];
do{
echo "[".key($a)."=>".pos($a)."]";
} while (next($a));
reset($a); echo "<br />";
print_r(each($a)); echo "<br />";
print_r(each($a)); echo "<br />";
print_r(each($a)); echo "<br />";
echo prev($a).prev($a).prev($a);
?>
</body></html>[0=>a][1=>b][2=>c][test=>d][3=>e][4=>f]
Array ( [1] => a [value] => a [0] => 0 [key] => 0 )
Array ( [1] => b [value] => b [0] => 1 [key] => 1 )
Array ( [1] => c [value] => c [0] => 2 [key] => 2 )
cba