多维结果数组需要使用PHP合并为具有相同键值的单个数组

时间:2016-05-19 13:51:18

标签: php arrays multidimensional-array merge

我有一个数组的多维结果需要使用PHP在单个数组中使用相同的键值进行合并,如下所示。

结果

Array
(
    [0] => Array
        (
            [POBI] => Array
                (
                    [average] => 3.9885520361991
                    [name] => POBI
                    [year] => 2014-2015
                )

            [POE] => Array
                (
                    [average] => 4
                    [name] => POE
                    [year] => 2014-2015
                )

            [LOS] => Array
                (
                    [average] => 4
                    [name] => LOS
                    [year] => 2014-2015
                )

            [Other Cop] => Array
                (
                )        

        )

    [1] => Array
        (
            [POBI] => Array
                (
                    [average] => 4
                    [name] => POBI
                    [year] => 2014-2015
                )

            [POE] => Array
                (
                    [average] => 3.9
                    [name] => POE
                    [year] => 2014-2015
                )

            [LOS] => Array
                (
                    [average] => 4
                    [name] => LOS
                    [year] => 2014-2015
                )

            [Other Cop] => Array
                (
                    [average] => 2
                    [name] => OC
                    [year] => 2014-2015
                )       

        )

    [1] => Array
        (
            [POBI] => Array
                (
                    [average] => 7
                    [name] => POBI
                    [year] => 2014-2015
                )

            [POE] => Array
                (
                    [average] => 3.0
                    [name] => POE
                    [year] => 2014-2015
                )

            [LOS] => Array
                (
                    [average] => 4
                    [name] => LOS
                    [year] => 2014-2015
                )

            [Other Cop] => Array
                (
                    [average] => 1.8
                    [name] => OC
                    [year] => 2014-2015
                )
        )



)

期望的结果

Array
(
        [POBI] => Array
        (
            [0] => Array
                (
                    [average] => 3.9885520361991
                    [name] => POBI
                    [year] => 2014-2015
                )

            [1] => Array
                (
                    [average] => 4
                    [name] => POBI
                    [year] => 2014-2015
                )

            [2] => Array
                (
                    [average] => 7
                    [name] => POBI
                    [year] => 2014-2015
                )    

        )

        [POE] => Array
        (
            [0] => Array
                (
                    [average] => 4
                    [name] => POE
                    [year] => 2014-2015
                )

            [1] => Array
                (
                    [average] => 3.9
                    [name] => POE
                    [year] => 2014-2015
                )

            [2] => Array
                (
                    [average] => 3.0
                    [name] => POE
                    [year] => 2014-2015
                )    

        )

        [LOS] => Array
        (
            [0] => Array
                (
                    [average] => 4
                    [name] => LOS
                    [year] => 2014-2015
                )

            [1] => Array
                (
                    [average] => 4
                    [name] => LOS
                    [year] => 2014-2015
                )

            [2] => Array
                (
                    [average] => 4
                    [name] => LOS
                    [year] => 2014-2015
                )    

        )

        [Other Cop] => Array
        (
            [0] => Array
                (

                )

            [1] => Array
                (
                    [average] => 2
                    [name] => OC
                    [year] => 2014-2015
                )

            [2] => Array
                (
                    [average] => 1.8
                    [name] => OC
                    [year] => 2014-2015
                )    

        )
) 

我目前的实施尝试做这样的事情:

foreach($data as $k => $v) {
    $results[$k] = array_column($arr, $k);
}
print_r($results);     

4 个答案:

答案 0 :(得分:1)

尝试这样的事情:

explode

答案 1 :(得分:1)

您的问题有一个简单的解决方案。实际上是一个班轮。

$mergedArray = array_merge_recursive($arrayOne, $arrayTwo);

// In your case
$merged = array_merge_recursive($array[0], $array[1]);

希望这可以解决问题。

修改

正如Brad Kent建议的那样,如果您有两个以上的值需要合并:

$merged = call_use_func_array('array_merge_recursive', array_values($array));

答案 2 :(得分:1)

试试这个:

foreach ($yourArrayName as $items)
{
    foreach ($items as $id => $item) {
        $result[$id][] = $item;     
    }
}
print_r($result);

注意:你的样本结果数据有1个重复索引...我猜应该是错误的。仅供参考。

答案 3 :(得分:0)

你关闭了。 尝试

$keys = array_keys($data[0]);
$results = array();
foreach ($keys as $key) {
    $results[$key] = array_column($data, $key);
}

这假设每个组都有相同的密钥。