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
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.