views:

35

answers:

2

How would I take an array such as this:

Array ( [key1] => item1 [key2] => item2 [key3] => item3 [key4] => item4 [key5] => item5 [key6] => item6 )

php:

$array = array('key1' => 'item1','key2' =>  'item2', 'key3' => 'item3','key4' =>  'item4', 'key5' => 'item5','key6' =>  'item6');

and create a multidimensional array with every 3 values as an array, such as:

Array ( [0] => Array ( [0] => item1 [1] => item2 [2] => item3 ) [1] => Array ( [0] => item4 [1] => item5 [2] => item6 ) )

php:

$array = array(array('item1','item2','item3'),
        array('item4','item5','item6'));

So that I can output the different items like: $newArray[0][1]

+3  A: 

array_chunk() - Split an array into chunks

$array = array('key1' => 'item1','key2' =>  'item2', 'key3' => 'item3','key4' =>  'item4', 'key5' => 'item5','key6' =>  'item6');
$a2 = array_chunk($array, 3);
echo $a2[0][1];
VolkerK
Hm. Thanks. That works.
Andrew
A: 
$new = array();
$vals = array_values($array);
for ($i = 0; $i < count($array); $i += 3) {
 $new[] = array_slice($vals, $i, 3);
}
Bart van Heukelom