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 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误