skip to Main Content

Have an array with this expression $products[] = $product[$category_id][$product_id]:

$products = array(
    33 => array(
        31 => array(
            'model' => 'Product 4',
            'product_id' => 31,
            'sku' => '',
            'ean' => '1234',
            'price' => '80.0000'
        ),
        8733 => array(
            'model' => 'qqq',
            'product_id' => 8733,
            'sku' => '',
            'ean' => '1000',
            'price' => '344.6281'
        )
    ),
    25 => array(
        30 => array(
            'model' => 'Product 3',
            'product_id' => 30,
            'sku' => '',
            'ean' => '250',
            'price' => '50.4132'
        ),
        31 => array(
            'model' => 'Product 4',
            'product_id' => 31,
            'sku' => '',
            'ean' => '1234',
            'price' => '80.0000'
        )
    )
);

I need remove duplicated product in php and result must be:

$products = array(
    33 => array(
        8733 => array(
            'model' => 'qqq',
            'product_id' => 8733,
            'sku' => '',
            'ean' => '1000',
            'price' => '344.6281'
        )
    ),
    25 => array(
        30 => array(
            'model' => 'Product 3',
            'product_id' => 30,
            'sku' => '',
            'ean' => '250',
            'price' => '50.4132'
        ),
        31 => array(
            'model' => 'Product 4',
            'product_id' => 31,
            'sku' => '',
            'ean' => '1234',
            'price' => '80.0000'
        ),
    )
);

I have tried this:

$cleared_products = array();

        foreach ($products as $category_id => $category_products) {
            foreach ($category_products as $product_id => $product) {
                if (isset($cleared_products[$category_id][$product_id])) {
                    
                    unset($products[$category_id][$product_id]);
                } else {
                    
                    $cleared_products[$category_id][$product_id] = $product;
                }
            }
        }

But does not work…

2

Answers


  1. The problem with your code is that you check if the product exists within it’s category. In your example the duplicate product is in two categories.

    We can adjust it easily by keeping track of the ID you added.

    $cleared_products = array();
    $productIds = array();
    
    foreach ($products as $category_id => $category_products) {
        foreach ($category_products as $product_id => $product) {
            if (in_array($product_id, $productIds)) {
                continue;
            } else {
                $productIds[] = $product_id;
                $cleared_products[$category_id][$product_id] = $product;
            }
        }
    }
    

    Important note: You don’t have control out of which category the item will be filtered

    So the expected result is not matched with this code. If you define the priority of categories and sort on this, you can delete it where wanted.

    Login or Signup to reply.
  2. Solution

    $products = array(
        33 => array(
            31 => array(
                'model' => 'Product 4',
                'product_id' => 31,
                'sku' => '',
                'ean' => '1234',
                'price' => '80.0000'
            ),
            8733 => array(
                'model' => 'qqq',
                'product_id' => 8733,
                'sku' => '',
                'ean' => '1000',
                'price' => '344.6281'
            )
        ),
        25 => array(
            30 => array(
                'model' => 'Product 3',
                'product_id' => 30,
                'sku' => '',
                'ean' => '250',
                'price' => '50.4132'
            ),
            31 => array(
                'model' => 'Product 4',
                'product_id' => 31,
                'sku' => '',
                'ean' => '1234',
                'price' => '80.0000'
            )
        )
    );
    
    // Create a new array to store unique products
    $uniqueProducts = array();
    
    foreach ($products as $parentKey => $items) {
        foreach ($items as $productId => $product) {
            $found = false;
            // Check if the product already exists in the uniqueProducts array
            foreach ($uniqueProducts as $existingItems) {
                if (isset($existingItems[$productId]) && $existingItems[$productId] == $product) {
                    $found = true;
                    break;
                }
            }
            // If the product is not found, add it to the uniqueProducts array
            if (!$found) {
                $uniqueProducts[$parentKey][$productId] = $product;
            }
        }
    }
    
    // Output the unique products array
    print_r($uniqueProducts);
    

    Output:

    Array
    (
        [33] => Array
            (
                [31] => Array
                    (
                        [model] => Product 4
                        [product_id] => 31
                        [sku] => 
                        [ean] => 1234
                        [price] => 80.0000
                    )
    
                [8733] => Array
                    (
                        [model] => qqq
                        [product_id] => 8733
                        [sku] => 
                        [ean] => 1000
                        [price] => 344.6281
                    )
    
            )
    
        [25] => Array
            (
                [30] => Array
                    (
                        [model] => Product 3
                        [product_id] => 30
                        [sku] => 
                        [ean] => 250
                        [price] => 50.4132
                    )
    
            )
    
    )
    

    Online Execution: 3v4l

    Explanation:

    1. Create a new array called $uniqueProducts to store the products without duplicates.
    2. Loop through each product in the original $products array.
    3. Check if the product already exists in $uniqueProducts by comparing its product_id and details.
    4. If the product isn’t found (i.e., it’s not a duplicate), add it to $uniqueProducts.
    5. The final result is an array with only unique products, keeping the same structure as the original array.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search