Arraywerte verschieben (Beispiel 2)

Weitere Beispiele

Beispiel ausführen
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($test1));
show_array(array_move($test2));
show_array(array_move($test3));
show_array(array_move($test4));


?>

Hinweise zum DatenschutzImpressum © 2005-2008 S. Eickhoff