从PHP数组中删除重复值并合并数量[重复]

fnatzsnv  于 2023-05-21  发布在  PHP
关注(0)|答案(2)|浏览(140)

此问题已在此处有答案

Group multidimensional array data based on two column values and sum values of one column in each group [duplicate](5个答案)
10小时前关闭。
我有一个名为$products_list的数组,其中填充了数据,结构如下:

$products_list[] = [
                'manufacturer' => $manufacturer_name,
                'mpn' => $mpn,
                'description' => $description,
                'quantity' => $quantity,
                'condition' => $condition,
                'price' => $price
];

我尝试从数组中删除任何重复的值,如果它们具有相同的description&condition,并合并重复的数量。
我尝试将数据设置为一个临时数组,以便能够在foreach语句中比较两个数组。就像这样:

foreach($products_list as $key => $row) {

            if($test_data['description'] == $row['description'] && $test_data['isbn'] == $isbn)
            {
                echo $row['description'];
                echo $row['quantity']+$temp_quantity;
            }
                $test_data = [
                'description' => $row['description'],
                'isbn' => $row['isbn'],
                'quantity' => 
            ];      
}
wmtdaxz3

wmtdaxz31#

下面是一个解决方案:

$products_list[] = [
    'manufacturer' => 'manufacturer1',
    'mpn' => 'mpn1',
    'description' => 'desc',
    'quantity' => 2,
    'condition' => 'condition',
    'price' => 10
];

$products_list[] = [
    'manufacturer' => 'manufacturer1',
    'mpn' => 'mpn1',
    'description' => 'desc',
    'quantity' => 3,
    'condition' => 'condition',
    'price' => 10
];

$products_list[] = [
    'manufacturer' => 'manufacturer2',
    'mpn' => 'mpn2',
    'description' => 'desc2',
    'quantity' => 4,
    'condition' => 'condition2',
    'price' => 15
];

$quantities   = [];

foreach ( $products_list as $product ) {
    $key = $product['description'].'|'.$product['condition']; // fields you want to compare
    if ( !isset($quantities[$key]) ) {
        $quantities[$key] = $product;
    } else {
        $quantities[$key]['quantity'] += $product['quantity'];
    }
}

$products   = array_values($quantities);

print_r($products);

结果就是

Array ( [0] => Array ( [manufacturer] => manufacturer1 [mpn] => mpn1 [description] => desc [quantity] => 5 [condition] => condition [price] => 10 ) [1] => Array ( [manufacturer] => manufacturer2 [mpn] => mpn2 [description] => desc2 [quantity] => 4 [condition] => condition2 [price] => 15 ) )
h9a6wy2h

h9a6wy2h2#

创建一个新的数组,而不是从原始数组中删除,您将使用唯一值填充该数组。

<?php
$products_list = [
    [
        'description' => 'foo',
        'condition' => 'foobar',
    ],
    [
        'description' => 'foo',
        'condition' => 'foobar',
    ],
    [
        'description' => 'hello',
        'condition' => 'hi',
    ]
];

$uniques = [];
foreach ($products_list as $productItem) {
    // loop through the uniques
    // if the `description` and `condition` match an existing one, don't add it
    foreach ($uniques as $uniqueItem) {
        if ($productItem['description'] == $uniqueItem['description']
            && $productItem['condition'] == $uniqueItem['condition']
        ) {
            // continue on both of the loops: 
            // 1) no need to loop over more unique items if you already found a match
            // 2) continuing of the `$products_list` loop will result in not adding the `productItem` to `$uniques`
            continue 2;
        }
    }
    
    $uniques[] = $productItem;
}

var_dump($uniques);

/*
array(2) {
  [0]=>
  array(2) {
    ["description"]=>
    string(3) "foo"
    ["condition"]=>
    string(6) "foobar"
  }
  [1]=>
  array(2) {
    ["description"]=>
    string(5) "hello"
    ["condition"]=>
    string(2) "hi"
  }
}

*/

相关问题