For example, I have array
// In
$arr = [0, 1, 2, 4, 5];
// Out
$cycled_arr = [5, 1, 2, 3, 4, 0];
I did this:
$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>";
Unfortunately it loading the page, be careful to execute!
Update:
// 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>";
But this makes a cycle once, although there is a null!
Try this function:
function cycleArray($times, $glArr){
for($i = 0; $i < $times; $i++)
{
//$glArr[] = array_shift($glArr);
array_unshift($glArr, array_pop($glArr));
}
return $glArr;
}
It probably gives you an error because cycleArray($arr);
doesn't exist. it needs 2 parameters.
Check your errorlog.
Assuming you actually mean cycle and not whatever it is your example shows:
function cycle(array $arr, $steps) {
return array_merge(array_slice($arr, $steps), array_slice($arr, 0, $steps));
}
var_dump(cycle(array(1, 2, 3, 4, 5), 3));
results in:
array(5) {
[0]=> int(4)
[1]=> int(5)
[2]=> int(1)
[3]=> int(2)
[4]=> int(3)
}