[英]How to combine 4 multidimensional arrays into 1 multidimensional
I unload 4 different tables from MySQL with different keys. 我用不同的键从MySQL卸载了4个不同的表。 I need to combine them into one array.
我需要将它们组合成一个数组。 I'll sort them by date (but it's not important and I know how to do it).
我将按日期对它们进行排序(但这并不重要,我知道该怎么做)。
As I see it: 照我看来:
foreach ($rows2 as $msgs2) {
$arraynew = array_merge($arraynew, array('cost' => $msgs2['vivod'], 'date' => $msgs2['date'], 'type' => '1'));
}
foreach ($rows3 as $msgs3) {
$arraynew = array_merge($arraynew, array('cost' => $msgs3['price'], 'date' => $msgs3['data'], 'type' => '2'));
}
foreach ($rows4 as $msgs4) {
$arraynew = array_merge($arraynew, array('cost' => $msgs4['price'], 'date' => $msgs4['data'], 'type' => '3'));
}
foreach ($rows5 as $msgs5) {
$arraynew = array_merge($arraynew, array('cost' => $msgs5['cost'], 'date' => $msgs5['data'], 'type' => '4'));
}
But it does not work. 但这行不通。
What you are looking for is array_merge_recursive
, 您正在寻找的是
array_merge_recursive
,
<?php
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($result);
?>
Output 输出量
Array
(
[color] => Array
(
[favorite] => Array
(
[0] => red
[1] => green
)
[0] => blue
)
[0] => 5
[1] => 10
)
use array_reduce($array, 'array_merge', array())
. 使用
array_reduce($array, 'array_merge', array())
。
Example: 例:
$a = array(array(1, 2, 3), array(4, 5, 6),array(7, 8, 9),array(10, 11, 12));
$result = array_reduce($a, 'array_merge', array());`
<?php
$foos = [
['vivod' => '11', 'date' => '20140112'],
['vivod' => '23', 'date' => '20140113']
];
$bars = [
['price' => '29', 'date' => '20171201'],
['price' => '31', 'date' => '20170102']
];
$result = array();
foreach ($foos as $foo) {
$result[] = array('cost' => $foo['vivod'], 'date' => $foo['date'], 'type' => '1');
}
foreach ($bars as $bar) {
$result[] = array('cost' => $bar['price'], 'date' => $bar['date'], 'type' => '2');
}
var_export($result);
Output: 输出:
array (
0 =>
array (
'cost' => '11',
'date' => '20140112',
'type' => '1',
),
1 =>
array (
'cost' => '23',
'date' => '20140113',
'type' => '1',
),
2 =>
array (
'cost' => '29',
'date' => '20171201',
'type' => '2',
),
3 =>
array (
'cost' => '31',
'date' => '20170102',
'type' => '2',
),
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.