合并三个不同长度的多维数组

时间:2018-08-30 08:20:36

标签: php arrays multidimensional-array merge

将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循环和数组合并,但是它并不能满足我的要求。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

从查看数据来看,“ DATE”似乎本身就是“关键”。考虑到这种逻辑,函数“ array_indexer”使用“ DATE”作为数组的键将现有的数据点(例如$ volume380)转换为索引数组。创建第二个函数“ 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;
}