例如,我有数组
// In
$arr = [0, 1, 2, 4, 5];
// Out
$cycled_arr = [5, 1, 2, 3, 4, 0];
我这样做了:
$arr = array(0, 1, 2, 3, 4, 5);
// It cycles once
function array_start_push($array){
return array_merge( array(array_pop($array)), $array);
}
// This function must cycle array $times times
function cycleArray($times,&$glArr){
if (!isset($times)){
$result = $glArr;
} else {
for ($loc = 0; $loc <= $times; $loc++) {
$result = array_start_push($glArr);
$glArr = $result;
}
return $result;
}
$hmm = cycleArray($arr);
echo "<pre>";
var_dump($hmm);
echo "</pre>";
不幸的是它正在加载页面,请小心执行!
更新:
// In array
$arr = array(0, 1, 2, 3, 4, 5);
// Circle array once
function array_start_push($array){
return array_merge( array(array_pop($array)), $array);
}
// Cycling $times times
function cycleArray($times,&$glArr){
for ($loc = 0; $loc <= $times; $loc++) {
$result = array_start_push($glArr);
$glArr = $result;
}
return $result;
}
$hmm = cycleArray(0,$arr);
echo "<pre>";
var_dump($hmm);
echo "</pre>";
但这会产生一次循环,尽管有一个空值!