Jump to content

Straight forward multidimensional array question


Vettel

Recommended Posts

I have an array with 192 items in it (0-191) and I want to rearrange them into a multidimensional array with 24 sets of 8.

 

If I wasn't very clear there, here's an abbreviated version of what I want:

 

$arr1 = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23);

$arr2 = array(array(0, 1, 2, 3, 4, 5, 6, 7), array(8, 9, 10, 11, 12, 13, 14, 15), array(16, 17, 18, 19, 20, 21, 22, 23));

 

Thank you in advance!

<?php
$arr1 = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23);
$i = 0;
$n = 0;
foreach($arr1 as $v) {
  $arr2[$n][] = $v;
  $n = ($i <  ? $n : ++$n;
  $i = ($i >7) ? 0 : ++$i;
}
echo '<pre>'; print_r($arr2); echo '</pre>';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.