<?php
$test = array(
'Affe',
'Bär',
'Camel',
'Dachs',
'Elefant',
'Fuchs',
'Giraffe',
'Hermelin',
'Igel',
'Jaguar',
'Koala',
'Lama',
'Manta',
);
function smart_sprintf($tpl, $data)
{
// Anzahl der Platzhalter ermitteln
$PlaceholderInTemplate = substr_count($tpl, '%s');
// Anzahl der Arrayelemente
$nbr = count($data);
$TotalRows = ceil($nbr/ $PlaceholderInTemplate);
// Array erweitern, damit die Anzahl aufgeht
$TotalNbrOfPlaceholder = $TotalRows * $PlaceholderInTemplate;
// Array erweitern
$EmptyValue = '-';
$data = array_pad($data, $TotalNbrOfPlaceholder, $EmptyValue);
$CurrentRow = 0;
while ($CurrentRow < $TotalRows) {
$offset = $CurrentRow * $PlaceholderInTemplate;
$part = array_slice($data, $offset, $PlaceholderInTemplate);
vprintf($tpl, $part);
$CurrentRow ++;
}
}
function array_move($data, $n, $empty_element= '1')
{
$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;
}
smart_sprintf('%s %s %s %s<br>', array_move($test,4));
echo '<hr>';
smart_sprintf('%s %s %s<br>', array_move($test,3));
?>