开发者

Creating a multidimensional array from row

开发者 https://www.devze.com 2022-12-14 16:58 出处:网络
How would I take an array such as this: Array ( [key1] => item1 [key2] => item2 [key3] => item3 [key4] => item4 [key5] => item5 [key6] => i开发者_StackOverflow中文版tem6 )

How would I take an array such as this:

Array ( [key1] => item1 [key2] => item2 [key3] => item3 [key4] => item4 [key5] => item5 [key6] => i开发者_StackOverflow中文版tem6 ) 

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]


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];


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

精彩评论

暂无评论...
验证码 换一张
取 消