0

I have an array called $products_list filled with data, with the following structure:

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

I am trying to remove any duplicate values from array if they have the same description && condition and to merge the quantity from duplicates.
I tried to to set the data into a temporary array for being able to compare the two arrays into a foreach statement. something like this:

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' => 
            ];      
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Kostasmel
  • 46
  • 8
  • 2
    Can you please share your attempt? – El_Vanja Mar 22 '21 at 14:48
  • 2
    1. Create *new* empty array 2. Iterate *current* array. 3. check if element from *current* array exists already in *new* one - if doesn't add it, if it does exist, modify the qty. – biesior Mar 22 '21 at 14:53

2 Answers2

2

Here is a solution :

$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);

And the result is

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 ) )
Yoleth
  • 1,269
  • 7
  • 15
1

Instead of removing from the original array, create a new one that you're going to fill with the unique values.

<?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"
  }
}

*/
Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • He wants to know how many times the unique item occurred which is not achieved with this solution. – Linek Mar 22 '21 at 15:15