如果您只想要一个永远循环的不断旋转的数组,则可以使用:
$array = range(1,5);
foreach($array as &$number) {
echo $number . PHP_EOL;
array_push($array, array_shift($array));
}
如果您希望它在每次页面加载时旋转,将前面的数字设置为一个变量,其余的在专用数组中排成一行,这样可以:
session_start();
if(!$_SESSION['loop_array']) {
$_SESSION['loop_array'] = range(1,5);
}
$current_value = array_shift($_SESSION['loop_array']);
$others_values = $_SESSION['loop_array'];
// Push current value to back of rotation, leaving next in line for
// next page load.
array_push($current_value, $_SESSION['loop_array']);
这也适用于以下数组(或任何数组):
$_SESSION['cute'] = array("dog", "cat", "pony", "bunny", "moose");
$_SESSION['ordinals'] = array("first", "second", "third", "fourth", "fifth");
$_SESSION['tick_tock_clock'] = array("I", "II", "III", "IV", "V", "VI",
"VII", "VIII", "IX", "X", "XI", "XII");
$_SESSION['randomness'] = array('butter', 'pillow', 'Alabama', 'bleeding gums');