Jump to content

Remove duplicates from multi dimensional array?


cyberkiller

Recommended Posts

Doing an API call and want to remove duplicates so I can display the products by brand. Problem I am having now is when I try to display the brands, I am getting duplicate brands. When I display the brands I am getting Nike, Nike, Reebook. When I just want, Nike, Reebok.

 

My Code

<?php
$results = array(); //array from API call

$results2 = $results['data'];

function multi_unique($array) {
        foreach ($array as $k=>$na)
            $new[$k] = serialize($na);
        $uniq = array_unique($new);
        foreach($uniq as $k=>$ser)
            $new1[$k] = unserialize($ser);
        return ($new1);
    }

$results3 = multi_unique($results2);

foreach ($results3 as $var)
{
echo $var['brand'];
?>
<br />
<?php
}

?>

The array

{
   "errors": [
   ],
   "warnings": [
   ],
   "data": [
      {
         "url": "http://[...]",
         "image_url": "[...]",
         "image_thumb_url": "[...]",
         "keyword": "Red Running Shoes",
         "description": "Red Running Shoes - Perfect for long jogs!",
         "category": "Shoes",
         "price": "59.99",
         "price_sale": null,
         "currency": "USD",
         "merchant": "",
         "brand": "Nike",
         "upc": "0944588778",
         "isbn": null,
         "sales": 23
      },
      {
         "url": "http://[...]",
         "image_url": "http://[...]",
         "image_thumb_url": null,
         "keyword": "Blue Running Shoes",
         "description": "Blue Running Shoes - Perfect for long jogs!",
         "category": "Shoes",
         "price": "59.99",
         "price_sale": "49.99",
         "currency": "USD",
         "merchant": "",
         "brand": "Nike",
         "upc": "0944588779",
         "isbn": null,
         "sales": 12
      }
{
         "url": "http://[...]",
         "image_url": "http://[...]",
         "image_thumb_url": null,
         "keyword": "Black Running Shoes",
         "description": "Black Running Shoes - Perfect for long jogs!",
         "category": "Shoes",
         "price": "59.99",
         "price_sale": "49.99",
         "currency": "USD",
         "merchant": "",
         "brand": "Reebok",
         "upc": "0944554779",
         "isbn": null,
         "sales": 12
      }
   ],
   "coupons": [
         
   ],
   "countryCode": "US",
   "page": 1,
   "limit": 10,
   "totalRecords": 3
}

That's doesn't sound like you want to remove duplicates. It sounds like you want to group same brand data together and process it in a specific way.

 

Do you have an example of the final result you are trying to produce?

What you posted works and is what I am looking for however I was going to also display the image_url with the brands.

 

End Result would be to display the list of brands with their associated image_url, but with no duplicate brands.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.