Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果值匹配,则在多级数组中聚合数组_Php_Arrays - Fatal编程技术网

Php 如果值匹配,则在多级数组中聚合数组

Php 如果值匹配,则在多级数组中聚合数组,php,arrays,Php,Arrays,我有一个以下格式的数组: array(5){ [0] => array(4){ ['product-id'] => 8931 ['product'] => 'Cake' ['description'] => 'Yellow cake' ['quantity'] => 1 } [1] => array(4){ ['product-id'] =&

我有一个以下格式的数组:

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
    }
}
如何比较所有阵列? 在我的代码中,我按产品ID对所有数组进行了排序,并编写了一个for来一次比较两行,但现在我明白了为什么这样做行不通

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;
}
函数cmp($a,$b){
返回strcmp($a[0],$b[0]);
}
函数组合数组($数组){
usort($阵列,“cmp”);
对于($i=1;$i
有什么更好的方法可以做到这一点


例如,要获取第一个数组并将其与下一个数组进行比较,请以值对值。只要前3个值中有一个不匹配,就可以将该行与下一行进行比较。如果前三个值都匹配,则将两个数组的quantity值相加,将其分配给第一个数组的quantity,然后去掉第二个数组。可能会有更多的匹配项,因此继续比较该数组,直到您完成整个列表。

您正在艰难地完成这项工作,为什么不这样做:

function combineProducts($products) {
    $result = array();
    foreach ($products as $product) {
        //if you can have different product or descriptions
        //per product id you can change this this to
        //$productId = implode('|', array($product['product-id'], $product['product'], $product['description']);
        $productId = $product['product-id'];
        //check if we already have this product
        if (isset($result[$productId])) {
            //add to the quantity
            $result[$productId]['quantity']+= $product['quantity'];
        } else {
            $result[$productId] = $product;
        }
    }
    //sort the results (remove if not needed)
    ksort($result);
    //return values (change to return $result; if you want an assoc array)
    return array_values($result);
}
试试这个

$arr=array(
    array(
        'product-id' => 8931,
        'product' => 'Cake',
        'description' => 'Yellow cake',
        'quantity' => 3,
    ),
    array(
        'product-id' => 8921,
        'product' => 'Cookies',
        'description' => 'Chocolate chip cookies',
        'quantity' => 2,
    ),  
    array(
        'product-id' => 8931,
        'product' => 'Cake',
        'description' => 'Yellow cake',
        'quantity' => 1,
    )      
);
$id = array();
foreach ($arr as $key => $row)
{
    $id[$key] = $row['product-id'];
}
array_multisort($id, SORT_DESC, $arr);

$result = array();
foreach ($arr as $key => $row)
{
    $pid = $row['product-id'];
    if(!isset($result[$pid]))
    {
        $result[$pid]=$row;
    }else{
        $result[$pid]['quantity']+=$row['quantity'];
    }
}
print_r($result);
我最喜欢的解决方案使用:


预期的结果是什么?在这个数组中,我们有3个数组用于产品黄饼。我想把数量值加起来,所以我没有现在的3个数组,而是有1个数组,数量是这3个数组的总和,这里是6。
$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()
);