0

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]

2 Answers 2

3

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];
Sign up to request clarification or add additional context in comments.

Comments

0
$new = array();
$vals = array_values($array);
for ($i = 0; $i < count($array); $i += 3) {
 $new[] = array_slice($vals, $i, 3);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.