dougong8012 2015-02-11 07:50
浏览 44
已采纳

如果它们的值匹配,则在多级数组中聚合数组

I have an array in the following format:

array(5){
    [0] =>
    array(4){
        ['product-id'] => 8931
        ['product'] => 'Cake'
        ['description'] => 'Yellow cake'
        ['quantity'] => 1
    }
    [1] =>
    array(4){
        ['product-id'] => 8921
        ['product'] => 'Cookies'
        ['description'] => 'Chocolate chip cookies'
        ['quantity'] => 2
    }   
    [2] =>     
    array(4){
        ['product-id'] => 8931
        ['product'] => 'Cake'
        ['description'] => 'Yellow cake'
        ['quantity'] => 1
    }        
    [3] =>
    array(4){
        ['product-id'] => 8931
        ['product'] => 'Cake'
        ['description'] => 'Yellow cake'
        ['quantity'] => 4
    }     
    [4] =>   
    array(4){
        ['product-id'] => 8933
        ['product'] => 'Cake'
        ['description'] => 'Chocolate cake'
        ['quantity'] => 1
    }
}

How can I compare all the arrays to each other? In my code I have sorted all the arrays by product ID, and written a for to compare two rows at a time, but now I see why that won't work.

function cmp($a, $b){
    return strcmp($a[0], $b[0]);
}

function combineArrays($arrays){
    usort($arrays, "cmp");
    for($i = 1; $i < count($arrays); $i++){
        $f_row = $arrays[$i];
        $next = $i + 1;
        $s_row = $arrays[$next];
        if($f_row[0] == $s_row[0]){
            if($f_row[1] == $s_row[1]){
                if($f_row[2] == $s_row[2]){
                                    $q1 = (int) $f_row[3];
                                    $q2 = (int) $s_row[3];
                                    $total = $q1 + $q2;
                                    unset($f_row[3]);
                                    $f_row[5] = $total;
                                    unset($arrays[$next]);
                }
            }
        }
    }
    return $arrays;
}

What is a better way to do this?

For example, to take the first array and compare it to the next, value for value. As soon as one of the first 3 values doesn't match, you go on to compare that row to the next one. If all of the first three values match, add up the quantity value of the two arrays, assign that to the first array's quantity, and get rid of the second array. There could be more matches, so continue comparing that array until you have gone through the whole list.

  • 写回答

3条回答 默认 最新

  • duanmian1085 2015-02-11 08:20
    关注

    My favorite solution uses array_reduce():

    $filtered = array_reduce(
        // Reduce the original list
        $arrays,
        // The callback function adds $item to $carry (the partial result)
        function (array $carry, array $item) {
            // Generate a key that contains the first 3 properties
            $key = $item['product-id'].'|'.$item['product'].'|'.$item['description'];
            // Search into the partial list generated until now
            if (array_key_exists($key, $carry)) {
                // Update the existing item
                $carry[$key]['quantity'] += $item['quantity'];
            } else {
                // Add the new item
                $carry[$key] = $item;
            }
            // The array_reduce() callback must return the updated $carry
            return $carry;
        },
        // Start with an empty list
        array()
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算