dqd54099 2016-09-02 20:21
浏览 46
已采纳

查找,组合并求和数组值

I have a array, it changes value every time when new data come (it's not from database). I want to calculate key 1 and key 2 value if the key 1 and key 3 matched even there is 5 arrays it returns value in one, but i am confused how to do that, here is the array I have:

    Array
    (
        [0] => Array
            (
                [0] => root
                [1] => 0.0
                [2] => 4428
                [3] => sshd
            )

        [1] => Array
            (
                [0] => root
                [1] => 12.5
                [2] => 1192
                [3] => sshd
            )

        [2] => Array
            (
                [0] => root
                [1] => 0.0
                [2] => 908
                [3] => udevd
            )

        [3] => Array
            (
                [0] => root
                [1] => 7
                [2] => 776
                [3] => udevd
            )

        [4] => Array
            (
                [0] => root
                [1] => 0.0
                [2] => 592
                [3] => mingetty
            )

        [5] => Array
            (
                [0] => root
                [1] => 2
                [2] => 592
                [3] => mingetty
            )

        [6] => Array
            (
                [0] => root
                [1] => 0.0
                [2] => 588
                [3] => mingetty
            )
        [7] => Array
            (
                [0] => root
                [1] => 0.0
                [2] => 0
                [3] => migration/0
            )

        [8] => Array
            (
                [0] => ntd
                [1] => 0.0
                [2] => 0
                [3] => ksoftirqd/0
            )

        [9] => Array
            (
                [0] => root
                [1] => 0.0
                [2] => 0
                [3] => ksoftirqd/0
            )

    }

I need an output like this:

    Array
    (
        [0] => Array
            (
                [0] => root
                [1] => 12.5
                [2] => 5620
                [3] => sshd
            )


        [1] => Array
            (
                [0] => root
                [1] => 7
                [2] => 1684
                [3] => udevd
            )

        [2] => Array
            (
                [0] => root
                [1] => 2
                [2] => 1772
                [3] => mingetty
            )


        [3] => Array
            (
                [0] => root
                [1] => 0.0
                [2] => 0
                [3] => migration/0
            )


        [4] => Array
            (
                [0] => ntd
                [1] => 0.0
                [2] => 0
                [3] => ksoftirqd/0
            )

        [5] => Array
            (
                [0] => root
                [1] => 0.0
                [2] => 0
                [3] => ksoftirqd/0
            )

    }

In detail:

I need to make a script/code to match all the array using key 0 and key 3 and both must be matched inside array.

Then we have to sum up key 1 value with all the others found array key 1 and key 2 value with all the others found key 2.

I hope I explain it clearly, I need a solution if possible don't post code just give me a guide how it's done.

I try different things which I think should work but I terribly fail.

  • 写回答

1条回答 默认 最新

  • dongyuli0964 2016-09-02 20:51
    关注

    Start with this simple foreach:

    $result_array = array();
    foreach ($start_array as $item) {
        // create a unique pair
        $key = $item[0] . ':' . $item[3];
    
        if (!isset($result_array[$key])) {
            // if we met this key first - add `$item` as initial value
            $result_array[$key] = $item;
        } else {
            // else just add needed data:
            $result_array[$key][1] += $item[1];
            $result_array[$key][2] += $item[2];
        }
    }
    print_r($result_array);
    // if you need numeric keys again - use `array_values()`.
    print_r(array_values($result_array));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用