- array_splice: documentation ( source)
- array_merge: documentation ( source)
- var_export: documentation ( source)
- range: documentation ( source)
<?php
function popUnshift(array $indexedArray, int $popShiftsCount): array
{
$count = count($indexedArray);
if ($count < 2) {
return $indexedArray;
}
$remainder = $popShiftsCount % $count;
if (!$remainder) {
return $indexedArray;
}
return array_merge(
array_splice($indexedArray, -$remainder),
$indexedArray
);
}
$A = [1, 2, 3, 4];
foreach (range(0, 9) as $K) {
var_export(popUnshift($A, $K));
echo "\n---\n";
}