I'm trying to divide an array into a multiple-dimensional array with 2 'elements' in each contained array.
So far I've only been able to divide them into a specified number of chunks, but as the number of elements are determined from a foreach loop and call to the database, I'm struggling to divide them into multiples of 2.
foreach ($_POST as $key)
{
$data[] = $key;
}
echo '<pre>';
print_r(partition($data, $i));
echo '</pre>';
function partition(Array $list, $p)
{
$listlen = count($list);
$partlen = floor($listlen / $p);
$partrem = $listlen % $p;
$partition = array();
$mark = 0;
for($px = 0; $px < $p; $px ++)
{
$incr = ($px < $partrem) ? $partlen + 1 : $partlen;
$partition[$px] = array_slice($list, $mark, $incr);
$mark += $incr;
}
return $partition;
}
The desired output would be like this ...
Array
(
[0] => Array
(
[0] => img.jpg
[1] => http://google.com
)
[1] => Array
(
[0] => img.jpg
[1] => http://google.com
)
[2] => Array
(
[0] => img.jpg
[1] => http://google.com
)
)
Any help would be appreciated.
Thanks