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!

Link to comment
Share on other sites

<?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>';
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.