cyberkiller Posted January 26, 2011 Share Posted January 26, 2011 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 } Quote Link to comment https://forums.phpfreaks.com/topic/225700-remove-duplicates-from-multi-dimensional-array/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 26, 2011 Share Posted January 26, 2011 $brands = array(); foreach($results['data'] as $arr){ $brands[] = $arr['brand']; // get list off all brand values } $brands = array_unique($brands); // unique list of brand names Quote Link to comment https://forums.phpfreaks.com/topic/225700-remove-duplicates-from-multi-dimensional-array/#findComment-1165359 Share on other sites More sharing options...
cyberkiller Posted January 26, 2011 Author Share Posted January 26, 2011 Is there anyway to remove the duplicate brands and maintain the rest of the data associated with the brands? After filtering 'brands' can I rejoin it back with the other data? img url, product name etc.... Quote Link to comment https://forums.phpfreaks.com/topic/225700-remove-duplicates-from-multi-dimensional-array/#findComment-1165361 Share on other sites More sharing options...
PFMaBiSmAd Posted January 26, 2011 Share Posted January 26, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/225700-remove-duplicates-from-multi-dimensional-array/#findComment-1165366 Share on other sites More sharing options...
cyberkiller Posted January 27, 2011 Author Share Posted January 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/225700-remove-duplicates-from-multi-dimensional-array/#findComment-1165870 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.