我有一个小问题,将3个不同长度的多维数组合并到一个数组中,没有重复键,如下所示:
我的3个阵列:
$volume380 = array {
array {
"DATE" => "2018-07-30"
"VOLUME380" => 813.0
}
array {
"DATE" => "2018-07-31"
"VOLUME380" => 1188.0
}
array {
"DATE" => "2018-08-01"
"VOLUME380" => 1081.0
}
array {
"DATE" => "2018-08-02"
"VOLUME380" => 886.0
}
}
$volume320 = array {
array {
"DATE" => "2018-07-30"
"VOLUME320" => 813.0
}
array {
"DATE" => "2018-07-31"
"VOLUME320" => 1188.0
}
array {
"DATE" => "2018-08-02"
"VOLUME320" => 1081.0
}
}
$volume800 = array {
array {
"DATE" => "2018-07-30"
"VOLUME800" => 813.0
}
array {
"DATE" => "2018-07-31"
"VOLUME800" => 1188.0
}
array {
"DATE" => "2018-08-01"
"VOLUME800" => 1081.0
}
array {
"DATE" => "2018-08-02"
"VOLUME800" => 886.0
}
}
我想要的结果:
$volumeTot = array {
array {
"DATE" => "2018-07-30"
"VOLUME380" => 813.0
"VOLUME320" => 813.0
"VOLUME800" => 813.0
}
array {
"DATE" => "2018-07-31"
"VOLUME380" => 1188.0
"VOLUME320" => 1188.0
"VOLUME800" => 1188.0
}
array {
"DATE" => "2018-08-01"
"VOLUME380" => 1081.0
"VOLUME800" => 1081.0
}
array {
"DATE" => "2018-08-02"
"VOLUME380" => 886.0
"VOLUME320" => 1081.0
"VOLUME800" => 886.0
}
}
我已经尝试了不同的方法来解决这个问题,但由于数组的长度不同,我有点不知所措。
我尝试过for循环和数组合并,但它没有做我想要的。
谢谢你的帮助。
从查看您的数据看,“DATE”似乎本身就是“关键”。考虑到这个逻辑,函数“array_indexer”将现有数据点(例如$ volume380)转换为索引数组,使用“DATE”作为数组的键。创建第二个函数“array_deindexer”以将数组格式恢复为原始数据所在的格式。
合并发生在函数“array_combiner”中,它“索引”给定的新数组和累积的数组,然后组合公共键(如“DATE”值)。
完整代码如下:
$volume380 = array (
array (
"DATE" => "2018-07-30",
"VOLUME380" => 813.0
),
array (
"DATE" => "2018-07-31",
"VOLUME380" => 1188.0
),
array (
"DATE" => "2018-08-01",
"VOLUME380" => 1081.0
),
array (
"DATE" => "2018-08-02",
"VOLUME380" => 886.0
)
);
$array_final = array_combiner($volume380);
$volume320 = array (
array (
"DATE" => "2018-07-30",
"VOLUME320" => 813.0
),
array (
"DATE" => "2018-07-31",
"VOLUME320" => 1188.0
),
array (
"DATE" => "2018-08-02",
"VOLUME320" => 1081.0
)
);
$array_final = array_combiner($volume320);
$volume800 = array (
array (
"DATE" => "2018-07-30",
"VOLUME800" => 813.0
),
array (
"DATE" => "2018-07-31",
"VOLUME800" => 1188.0
),
array (
"DATE" => "2018-08-01",
"VOLUME800" => 1081.0
),
array (
"DATE" => "2018-08-02",
"VOLUME800" => 886.0
)
);
$array_final = array_combiner($volume800);
print_r($array_final);
function array_combiner($array_tmp) {
global $array_final;
$array_tmp = array_indexer($array_tmp);
$tmp = array();
if (count($array_final) > 0) {
$tmp = array_indexer($array_final);
foreach ($array_tmp as $k1 => $v1) {
if (!isset($tmp[$k1])) $tmp[$k1] = array();
foreach ($v1 as $k2 => $v2) {
$tmp[$k1][$k2] = $v2;
}
}
} else $tmp = $array_tmp;
return array_deindexer($tmp);
}
function array_indexer($array_tmp) {
$tmp = array();
foreach ($array_tmp as $k1 => $v1) {
$index = "";
$data = array();
foreach ($v1 as $k2 => $v2) {
if ($k2 == "DATE") $index = $v2;
else $data[$k2] = $v2;
}
if (strlen($index) > 0) $tmp[$index] = $data;
}
return $tmp;
}
function array_deindexer($array_tmp) {
$tmp = array();
$i = 0;
foreach ($array_tmp as $k1 => $v1) {
$tmp[$i]["DATE"] = $k1;
foreach ($v1 as $k2 => $v2) $tmp[$i][$k2] = $v2;
$i++;
}
return $tmp;
}