问题
I have three array like this:
array1(
0=>'title 1',
1=>'title 2'
)
array2(
0=>'description 1',
1=>'description 2'
)
array3(
0=>'price 1',
1=>'price 2'
)
There is a php function to grouping array values by keys like this?
array(
0=>array(title 1, description 1, price 1),
1=>array(title 2, description 2, price 2),
)
回答1:
array_map(null, $array1, $array2, $array3)
See http://php.net/manual/en/function.array-map.php example #4.
回答2:
$pool =array();
foreach ( array_map(null, $array1, $array2, $array3) as $key => $value) {
$pool[$key] = implode(", ", $value);
}
print_r($pool);
Result :
Array
(
[0] => title 1, description 1, price 1
[1] => title 2, description 2, price 2
)
回答3:
Try this code.
$array3 = array();
foreach ( $array1 as $key => $val ) {
if ( !isset($array3[$val]) )
$array3[$val] = array();
$array3[$val][] = $array2[$key];
}
print_r($array3);
来源:https://stackoverflow.com/questions/21512889/php-grouping-array-values-by-keys