PHP Merge数组具有相同的键和一个相同的值

时间:2014-02-13 18:19:15

标签: php arrays

我需要合并一个PHP数组,这个数组有2个数组,名为“targetXX”,我可以有2个或更多。每个目标都有相同的键,对于每个键,我有一个带有2个值a和b的数组,两个目标中的a总是相同的,但是我需要像这样合并两个B值:

Array
(
    [0] => Array
        (
            [target] => hitcount(stats.asdf1.requests, "1min")
            [datapoints] => Array
                (
                    [0] => Array
                        (
                            [0] => 1200
                            [1] => 1392282200
                        )

                    [1] => Array
                        (
                            [0] => 1400
                            [1] => 1392282260
                        )

                    [2] => Array
                        (
                            [0] => 600
                            [1] => 1392282320
                        )

                    [3] => Array
                        (
                            [0] => 200
                            [1] => 1392282380
                        )

                    [4] => Array
                        (
                            [0] => 400
                            [1] => 1392282440
                        )

                    [5] => Array
                        (
                            [0] => 600
                            [1] => 1392282500
                        )
                )
        )
    [1] => Array
        (
            [target] => hitcount(stats.asdf.requests, "1min")
            [datapoints] => Array
                (
                    [0] => Array
                        (
                            [0] => 4321
                            [1] => 1392282200
                        )

                    [1] => Array
                        (
                            [0] => 76567
                            [1] => 1392282260
                        )

                    [2] => Array
                        (
                            [0] => 5556
                            [1] => 1392282320
                        )

                    [3] => Array
                        (
                            [0] => 7675
                            [1] => 1392282380
                        )

                    [4] => Array
                        (
                            [0] => 2344
                            [1] => 1392282440
                        )

                    [5] => Array
                        (
                            [0] => 0999
                            [1] => 1392282500
                        )
                )
        )

结果:

    Array
(
    [0] => Array
        (
            [target] => hitcount(stats.asdf1.requests, "1min")
            [datapoints] => Array
                (
                    [0] => Array
                        (
                            [0] => 1200
                            [1] => 1392282200
                            [2] => 4321
                        )

                    [1] => Array
                        (
                            [0] => 1400
                            [1] => 1392282260
                            [2] => 76567
                        )

                    [2] => Array
                        (
                            [0] => 600
                            [1] => 1392282320
                            [2] => 5556
                        )

                    [3] => Array
                        (
                            [0] => 200
                            [1] => 1392282380
                            [2] => 7675
                        )

                    [4] => Array
                        (
                            [0] => 400
                            [1] => 1392282440
                            [2] => 2344
                        )

                    [5] => Array
                        (
                            [0] => 600
                            [1] => 1392282500
                            [2] => 0999
                        )
                )
        )

2 个答案:

答案 0 :(得分:2)

内置function array_merge可能会为您完成工作。实际上,您需要合并每个子数组,因为array_merge_recursive函数不处理索引。

$newArray = array();
foreach ($myArray['target2'] as $key => $arr) {
    $newArray['target'][$key] = array_merge($myArray['target1'][$key], $arr[1]);
}
  

将一个或多个数组的元素合并在一起,以便将一个值的值附加到前一个数组的末尾。它返回结果数组。

     

如果输入数组具有相同的字符串键,则该键的后一个值将覆盖前一个键。但是,如果数组包含数字键,则后面的值不会覆盖原始值,但会附加。

如果要合并的密钥超过2个,则可以多次循环算法。

答案 1 :(得分:2)

使用array_merge()来实现这一目标:

$newArray = array();

foreach ($myArray['target2'] as $key => $innerArr1) {
    $newArray['target'][$key] = array_merge(
        $myArray['target1'][$key],  /* 0th and 1st index */
        array($innerArr1[1])        /* 2nd index         */
    );
}

print_r($newArray);

输出:

Array
(
    [target] => Array
        (
            [0] => Array
                (
                    [0] => 333333
                    [1] => 13
                    [2] => 99
                )

            [1] => Array
                (
                    [0] => 444444
                    [1] => 15
                    [2] => 98
                )

            [2] => Array
                (
                    [0] => 555555
                    [1] => 17
                    [2] => 97
                )

        )

)

Demo