U kunt de onderstaande functie gebruiken en de verschuivingsparameter eraan doorgeven. De functienaam is roteer_array() hieronder. Voeg na gebruik van de functie de volgende waarde toe.
<?php
$weekdays = array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
function rotate_array($steps, $arr)
{
if($steps >= 0)
{
for($i = 0; $i < $steps; $i++)
{
$elm = array_shift($arr);
array_push($arr, $elm);
}
}
else
{
for($i = 0; $i > $steps; $i--)
{
$elm = array_pop($arr);
array_unshift($arr, $elm);
}
}
return $arr;
}
$arr = rotate_array(1, $weekdays);
//For multidimensional array, add this part
foreach($arr as $key => $childArray) {
rotate_array(1,$childArray);
}
//end of addition
$arr[] = "next value";
echo "<pre>";
print_r($arr);
echo "</pre>";
?>
";?> Uitgang:
Array
(
[0] => Tue
[1] => Wed
[2] => Thu
[3] => Fri
[4] => Sat
[5] => Sun
[6] => Mon
[7] => next value
)