duannao8450 2010-10-07 23:49
浏览 41
已采纳

计算多维数组中的不同值

I have an array that looks like the one below. I'm trying to group and count them, but haven't been able to get it to work.

The original $result array looks like this:

Array
(
    [sku] => Array
        (
            [0] => 344
            [1] => 344
            [2] => 164
        )

    [cpk] => Array
        (
            [0] => d456
            [1] => d456
        )
)

I'm trying to take this and create a new array:

$item[sku][344] = 2;
$item[sku][164] = 1;
$item[cpk][d456] = 1;

I've gone through various iterations of in_array statements inside for loops, but still haven't been able to get it working. Can anyone help?

  • 写回答

1条回答 默认 最新

  • double0201 2010-10-07 23:58
    关注

    I wouldn't use in_array() personally here.

    This just loops through creating the array as it goes.

    It seems to work without needing to first set the index as 0.

    $newArray = array();
    
    foreach($result as $key => $group) {   
        foreach($group as $member) {
            $newArray[$key][$member]++;
        }    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求Houdini使用行家,付费。价格面议。
  • ¥15 AttributeError: 'EasyDict' object has no attribute 'BACKUP_DB_INFO'
  • ¥15 前端高拍仪调用问题报错
  • ¥15 想用octave解决这个数学问题
  • ¥15 Centos新建的临时ip无法上网,如何解决?
  • ¥15 海康威视如何实现客户端软件对设备语音请求的处理。
  • ¥15 支付宝h5参数如何实现跳转
  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 CF1927D 求Hack