在php中基于key对数组值进行分组,并计算分组产品的数量,

14ifxucb  于 2022-12-02  发布在  PHP
关注(0)|答案(2)|浏览(113)

也许有人已经问过了,但我没有找到正确的答案。我需要按mpnproduct_id键值对数组进行分组,并计算其数量。我的数组:

[0] => Array
        (
            [product] => Product HTC
            [mpn] => 
            [quantity] => 3
            [product_id] => 28
        )

    [1] => Array
        (
            [product] => Product HTC
            [mpn] => ggg
            [quantity] => 5
            [product_id] => 28
        )

    [2] => Array
        (
            [product] => Product HTC
            [mpn] => ggg
            [quantity] => 1
            [product_id] => 28
        )

    [3] => Array
        (
            [product] => Product HTC
            [mpn] => ggg
            [quantity] => 1
            [product_id] => 28
        )

    [4] => Array
        (
            [product] => Product HTC
            [mpn] => fff
            [quantity] => 1
            [product_id] => 28
        )

期望的结果:

[0] => Array
    (
        [product] => Product HTC
        [mpn] => 
        [quantity] => 3
        [product_id] => 28
    )

[1] => Array
    (
        [product] => Product HTC
        [mpn] => ggg
        [quantity] => 7
        [product_id] => 28
    )

[2] => Array
    (
        [product] => Product HTC
        [mpn] => fff
        [quantity] => 1
        [product_id] => 28
    )

我尝试过这个建议Group array values based on key in php?,但没有成功。

4jb9z9bj

4jb9z9bj1#

通过这段代码,你可以得到一个按mpn分组的数组。其中**$myList**是你的原始数组。

// For Each Element
foreach ($myList as $myKey => $myValue)
{
    // Define New Key
    $newKey = $myValue["mpn"];

    // You can Define New Key with Concatenation of Multiple Element
    // Ex : $newKey = $myValue["mpn"]."_".$myValue["product_id"];

    // If Never Memorised OR Already Memorised
    if(!array_key_exists($newKey,$newList)) $newList["$newKey"] = $myValue;
    else $newList["$newKey"]["quantity"] = bcadd($newList["$newKey"]["quantity"],$myValue["quantity"],0);
}
// End - For Each Element

// Display Result
echo "<pre>"; print_r($newList); echo "</pre>";
8yparm6h

8yparm6h2#

下面是一个使用简单循环的工作示例。

<?php
function group_by_mpn($array){
    $mpn = array();
    $result = array();
    foreach($array as $key => $value){
        if(!in_array($value['mpn'], $mpn)){
            $mpn[] = $value['mpn'];
            $result[] = $value;
        }else{
            $index = array_search($value['mpn'], $mpn);
            $result[$index]['quantity'] = $result[$index]['quantity'] + $value['quantity'];
        }
    }
    return $result;
}
$array = array(
    array('product' => 'Product HTC', 'mpn' => '', 'quantity' => 3, 'product_id' => 28),
    array('product' => 'Product HTC', 'mpn' => 'ggg', 'quantity' => 5, 'product_id' => 28),
    array('product' => 'Product HTC', 'mpn' => 'ggg', 'quantity' => 1, 'product_id' => 28),
    array('product' => 'Product HTC', 'mpn' => 'ggg', 'quantity' => 1, 'product_id' => 28),
    array('product' => 'Product HTC', 'mpn' => 'fff', 'quantity' => 1, 'product_id' => 28),
);

$result = group_by_mpn($array);
echo json_encode($result);

?>

相关问题