0

Ok I have an multidimensional array

Array(
    [items] [0] array(
             [0] array("name"=>"something", "price"=>"0.00", "desc"=>"blah blah blah"),
             [1] array("name"=>"other", "price"=>"0.00", "desc"=>"blah blah blah"),
             [2] array("name"=>"monkey", "price"=>"0.00", "desc"=>"blah blah blah"),
             [3] array("name"=>"something", "price"=>"0.00", "desc"=>"blah blah blah"),
             [4] array("name"=>"suit", "price"=>"0.00", "desc"=>"blah blah blah")
    ),
    [categories] [0] array("outter", "inner", "summer", "fall"),
    [subcategories] [0] array("outter", "inner", "summer", "fall")
)

(ok its a poor hand typed rendition but you get the idea). What My concern is, is the "items" if there is more than one name thats the same I want to remove that entry, but Im really not sure how I would go about achieving this properly without concocting some large ugly set up loops. So I am trying to get some help coming up with something thats fast, optimized, and well cleaner than what I am likely to come up with for an idea.

chris
  • 36,115
  • 52
  • 143
  • 252
  • Did you try to search before asking. I see lots of similar questions. Like: http://stackoverflow.com/questions/1861682/php-multi-dimensional-array-remove-duplicate – Sudhir Bastakoti Mar 06 '12 at 06:03

1 Answers1

1

OK, it's one loop. But what about switching the array out temporarily for an associative one and pulling the values?

$assoc_items = array();

foreach($items as $item) {
  $assoc_items[$item['name']] = $item;
}

$result = array_values($assoc_items);

result now contains a list of the items from $items with unique names.

rjz
  • 16,182
  • 3
  • 36
  • 35