dongshi1148 2014-12-12 18:00
浏览 76
已采纳

合并或组合多维数组的维度

I've got this array

Array
(
    [0] => Array
        (
            [Browser] => Chrome
            [Number_Browser] => 3
            [Platform] => MacOSX
        )

    [1] => Array
        (
            [Browser] => Default Browser
            [Number_Browser] => 10187
            [Platform] => unknown
        )

    [2] => Array
        (
            [Browser] => Chrome
            [Number_Browser] => 2
            [Platform] => MacOSX
        )

    [3] => Array
        (
            [Browser] => Chrome
            [Number_Browser] => 1
            [Platform] => MacOSX
        )

    [4] => Array
        (
            [Browser] => Chrome
            [Number_Browser] => 2
            [Platform] => MacOSX
        )

    [5] => Array
        (
            [Browser] => Chrome
            [Number_Browser] => 1
            [Platform] => MacOSX
        )

    [6] => Array
        (
            [Browser] => Chrome
            [Number_Browser] => 1
            [Platform] => MacOSX
        )

    [7] => Array
        (
            [Browser] => Default Browser
            [Number_Browser] => 1
            [Platform] => unknown
        )

    [8] => Array
        (
            [Browser] => Chrome
            [Number_Browser] => 4
            [Platform] => MacOSX
        )

    [9] => Array
        (
            [Browser] => Safari
            [Number_Browser] => 1
            [Platform] => MacOSX
        )

)

This is what I need to get as a result

Array
(
    [0] => Array
        (
            [Browser] => Chrome
            [Number_Browser] => 14
            [Platform] => MacOSX
        )

    [1] => Array
        (
            [Browser] => Default Browser
            [Number_Browser] => 10188
            [Platform] => unknown
        )

    [3] => Array
        (
            [Browser] => Safari
            [Number_Browser] => 1
            [Platform] => MacOSX
        )

)

I've been trying for a while with no luck. I need to combine the sub dimensions so when displaying the list on screen it displays the correct amount.

This data is coming from DB, which in Browser we get the User agent, so that's why there are a number of Chrome in the result because of different versions or User Agent string.

Then I'm using browser cap to make it easier to understand to the end user.

I would like then to combine the arrays so it give me the result as described above.

Any help would be appreciated.

  • 写回答

1条回答 默认 最新

  • douzhang1852 2014-12-12 18:08
    关注

    Can try using foreach(). Use an temporary array to set calculation results. Example:

    $your_arr = Array
    (
    Array
    (
        "Browser" => "Chrome",
        "Number_Browser" => 3,
        "Platform" => "MacOSX",
    ),
    
    Array
    (
        "Browser" => "Default Browser",
        "Number_Browser" => 3,
        "Platform" => "MacOSX",
    ),
    
    Array
    (
        "Browser" => "Chrome",
        "Number_Browser" => 3,
        "Platform" => "MacOSX",
    ),
    //...............
    );
    
    
    $newArr = array();
    foreach($your_arr as $key=>$val){
        $index = $val['Browser'];
        if(isset($newArr[$index])){
            $val_0 = $newArr[$index]['Number_Browser'] + $val['Number_Browser'];
            $newArr[$index] = array('Browser'=>$val['Browser'], "Number_Browser" => $val_0, 'Platform'=>$val['Platform']);
        }else{
            $newArr[$index] = $val;
        }
    }
    $result = array_values($newArr);
    
    print '<pre>';
    print_r($result);
    print '</pre>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大