与多维数组具有相同值的两个索引的总和

时间:2017-07-28 07:02:32

标签: php arrays

我正在使用多维数组,如下面的php,

$return_array=   Array
    (
        [0] => Array
            (
                [0] => 3_Mar_2017
                [1] => 0
                [2] => 19
                [3] => 7
                [4] => 13
                [5] => 3
                [6] => 0
                [7] => 42
            )

        [1] => Array
            (
                [0] => Yet to closed
                [1] => 0
                [2] => 0
                [3] => 0
                [4] => 0
                [5] => 1
                [6] => 0
                [7] => 1
            )

        [2] => Array
            (
                [0] => 3_Mar_2017
                [1] => 0
                [2] => 7
                [3] => 0
                [4] => 0
                [5] => 0
                [6] => 0
                [7] => 7
            )

        [3] => Array
            (
                [0] => 4_Apr_2017
                [1] => 0
                [2] => 8
                [3] => 4
                [4] => 0
                [5] => 0
                [6] => 0
                [7] => 12
            )

      )

在第0和第2个索引上 - >从索引的子数组 - >第0个索引是常见的" 3_Mar_2017 " ,我想总结那两个索引,并希望得到如下所示的结果,

 $final_return = Array
        (
            [0] => Array
                (
                    [0] => 3_Mar_2017
                    [1] => 0
                    [2] => 26
                    [3] => 7
                    [4] => 13
                    [5] => 3
                    [6] => 0
                    [7] => 49
                )

            [1] => Array
                (
                    [0] => Yet to closed
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                    [5] => 1
                    [6] => 0
                    [7] => 1
                )    
            [2] => Array
                (
                    [0] => 4_Apr_2017
                    [1] => 0
                    [2] => 8
                    [3] => 4
                    [4] => 0
                    [5] => 0
                    [6] => 0
                    [7] => 12
                )

          )

我尝试过的循环代码如下,

 $tem_array = array();
        $final_return = array();
        foreach ($return_array as $unique) {
            if (!in_array($unique[0], $tem_array)) {
                array_push($tem_array, $unique[0]);
                $final_return[] = $unique;
            } else {
                $index = array_search($unique[0], $tem_array);
                for ($i = 1; $i < count($unique); $i++) {
                    $final_return[$index][$i] = $final_return[$index][$i] + $unique[$i];
                }
            }
        }

但是如果数组大小那么,可能需要时间才有任何简单的解决方案。 任何人都可以用最少的代码帮助我获得这个必需的结果吗? 我会很感激最好的答案。

1 个答案:

答案 0 :(得分:1)

希望这就是你要找的东西

$temp1 = array(); $result = array(); 
foreach ($myArray as $temp) { 
    if (!in_array($temp[0], $temp1)) { 
        array_push($temp1, $temp[0]); $result[] = $temp; 
    } else { 
        $id = array_search($temp[0], $temp1); for ($i = 1; $i <= count($temp); $i++) { 
        $result[$id][$i] = $result[$id][$i] + $temp[$i]; 
        } 
    } 
} 

你的第一个数组看起来像

Array
(
    [0] => Array
        (
            [0] => 13
            [1] => 1
            [2] => 2
            [3] => 3
        )

    [1] => Array
        (
            [0] => 14
            [1] => 5
            [2] => 6
            [3] => 7
        )

    [2] => Array
        (
            [0] => 13
            [1] => 1
            [2] => 2
            [3] => 3
        )

)

,结果如

Array
(
    [0] => Array
        (
            [0] => 13
            [1] => 2
            [2] => 4
            [3] => 6
        )

    [1] => Array
        (
            [0] => 14
            [1] => 5
            [2] => 6
            [3] => 7
        )

)