Verschiebt Arrayelemente um vorgegebenen Wert.
<?php
$test = array(
'Affe',
'Bär',
'Camel',
'Dachs',
'Elefant',
'Fuchs',
'Giraffe',
'Hermelin',
'Igel',
'Jaguar',
'Koala',
'Lama',
'Manta',
);
function show_array($arr)
{
echo '<hr>'.join(', ', $arr);
}
function array_move($data, $n, $empty_element= '-')
{
$tmp = array();
$i = 0;
$nbr_of_elements = count($data);
$nbr_of_rows = ceil($nbr_of_elements/ $n);
// Array erweitern, damit die Anzahl aufgeht
$m = $nbr_of_rows * $n;
$data = array_pad($data, $m, $empty_element);
foreach ($data as $v) {
$pos = floor ($i/ $nbr_of_rows) + ($i % $nbr_of_rows)*$n;
$i++;
$tmp[$pos] = $v;
}
ksort($tmp);
return $tmp;
}
show_array(array_move($test, 1));
show_array(array_move($test, 2));
show_array(array_move($test, 3));
show_array(array_move($test, 4));
?>