-2

I'm not sure why this is getting negative reputation. The suggested duplicate questions only deal with identical items in the array. I'm looking for arrays that have the same value for just one item in that array.

Here is my question:

I need to remove items from a multidimensional array based on the value of one of the keys. Here's a sample of the array. The actual array has 3,000 items.

 Array
    (
        [0] => Array
            (
                [ID] => 1553
                [Title] => Red Mini Dress
                [Size] => 8
                [Availability] => In stock
            )
        [1] => Array
            (
                [ID] => 1554
                [Title] => Red Mini Dress
                [Size] => 10
                [Availability] => Out of stock
            )
        [2] => Array
            (
                [ID] => 1555
                [Title] => Red Mini Dress
                [Size] => 12
                [Availability] => In stock
            )
        [3] => Array
            (
                [ID] => 2430
                [Title] => Black Cotton Trousers
                [Size] => 14
                [Availability] => In stock
            )
        ...
    )

So you can see that some items, like "Red Mini Dress", are listed several times. This is because there is an entry for every size of that item.

You can also see that sizes 8 and 12 of "Red Mini Dress" are in stock, but not size 10.

Now, I need to reduce the array to show just one entry for each product. And the value of Availability should be In stock unless all sizes are out of stock.

So, for the above example, there should be only one entry for "Red Mini Dress", and it should be In stock.

Edit

So far I've tried using this method, which sorts the array based on the size of the ID:

function reduce_products($items) {
    $reduced = array();
    for($i = 0; $i < sizeof($items); $i++) {
            if (!array_key_exists($items[$i]['id'], $reduced) || $reduced[$items[$i]['id']]['title'] > $items[$i]['title'])
            $reduced[$items[$i]['id']] = $items[$i];
    }
    return $reduced;
}

The code is adapted from Removing duplicate values in Multi-Dimensional Array?

But that doesn't remove any values.

Sean H
  • 1,045
  • 1
  • 14
  • 32
  • I would start by looking at improving the query that generated this result – RiggsFolly Jul 11 '17 at 11:32
  • 1
    Please read [What topics can I ask about](http://stackoverflow.com/help/on-topic) and [How to ask a good question](http://stackoverflow.com/help/how-to-ask) and [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and how to create a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) **SO is not** a free Coding service ___We try to fix your code, we do not write your code___ – RiggsFolly Jul 11 '17 at 11:33
  • Unless you want this question closed as Too Broad, it would be a good idea to show the code you have tried to write to achieve this _requirement specification_ – RiggsFolly Jul 11 '17 at 11:34
  • Possible duplicate of [How to remove duplicate values from a multi-dimensional array in PHP](https://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php) – Salketer Jul 11 '17 at 11:36
  • What should `Size` and '`ID` be when there are multiple sizes of the same `Title` (e.g., "Red Mini Dress"? – bill.lee Jul 11 '17 at 11:38
  • Can people please stop downvoting this? It's not a duplicate of this question: https://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php because that only deals with identical items in the array. I'm looking for arrays that have the same value for just one item in that array. – Sean H Jul 11 '17 at 11:48
  • @wrlee I don't mind – Sean H Jul 11 '17 at 11:48

2 Answers2

0

PHP 5.5+, you can use array_column.

Script

<?php 
$arr= array( 
           array( 'Id' => 111,
                  'Title' => "foo", 
                  'Number' => 15 
                ),
           array( 'Id' => 111,
                  'Title' => "foo", 
                  'Number' => 15 
                ),
           array( 'Id' => 125,
                  'Title' => "bar", 
                  'Number' => 15 
                )
         );

      var_dump(array_unique(array_column($arr, 'Id')));


?>

Output:

array(2) {
  [0]=>
  int(111)
  [2]=>
  int(125)
}
Syed Daniyal Asif
  • 726
  • 1
  • 5
  • 19
0

try this:

<?php

$array = [
            [
                'ID'           => 1553,
                'Title'        => 'Red Mini Dress',
                'Size'         => 8,
                'Availability' => 'In stock',
            ],
            [
                'ID'           => 1554,
                'Title'        => 'Red Mini Dress',
                'Size'         => 10,
                'Availability' => 'Out of stock',
            ],
            [
                'ID'           => 1555,
                'Title'        => 'Red Mini Dress',
                'Size'         => 12,
                'Availability' => 'In stock',
            ],
            [
                'ID'           => 2430,
                'Title'        => 'Black Cotton Trousers',
                'Size'         => 14,
                'Availability' => 'In stock',
            ],
        ];

$result = [];
foreach($array as $element) {
    $result_title = array_column($result, 'Title');

    if(!in_array($element['Title'], $result_title)) {
        $result[] = $element;
        continue;
    }

    foreach($result as $key => $value) {
        if(
            $value['Title'] === $element['Title']
         && 'Out of stock' === $value['Availability']
         && 'In stock' === $element['Availability']
        ) {
            $result[$key] = $element;
            break;
        }
    }
}

echo '<pre>';
print_r($result);

output:

Array
(
    [0] => Array
        (
            [ID] => 1553
            [Title] => Red Mini Dress
            [Size] => 8
            [Availability] => In stock
        )

    [1] => Array
        (
            [ID] => 2430
            [Title] => Black Cotton Trousers
            [Size] => 14
            [Availability] => In stock
        )

)
yoeunes
  • 2,927
  • 2
  • 15
  • 26
  • You've ignored the 'availability' part of the question. – Sean H Aug 16 '17 at 08:38
  • no sir see here : `if( $value['Title'] === $element['Title'] && 'Out of stock' === $value['Availability'] && 'In stock' === $element['Availability'] )` – yoeunes Aug 16 '17 at 08:39
  • @Seano if the existing value of the result array has an availability `Out of stock` and the new one `In stock` i replace the old by the new one. let me know if there is something else – yoeunes Aug 16 '17 at 08:43
  • @Seano you can see now the complete code without the scroll bar – yoeunes Aug 16 '17 at 09:04