-1

My array is something like this:

   $sth = array(
        array("group" => "1", "name" => "John", "whatever" => "a"),
        array("group" => "1", "name" => "John", "whatever" => "b"), // this is the duplicate I want to remove
        array("group" => "1", "name" => "Henry", "whatever" => "c"),
        array("group" => "2", "name" => "John", "whatever" => "d"),
        array("group" => "2", "name" => "Peter", "whatever" => "e")
    )

And I want to remove the duplicate 1/John entry, but not the 2/John since it's not a duplicate. I only want to check those two values, not the "whatever" value, since that will always be different. I know I could use in_array to search for just "John", or array_search to check the keys, but the keys don't always correlate to the name, so I need to only remove duplicates where the key/value are the exact same. I'm thinking something like:

$cleaned_sth = array();
foreach($sth as $s) {
    $group = $s[0];
    $name = $s[1];
    if( in_array($group => $name, $cleaned_sth ) continue;
    else $cleaned_sth[] = array("group" => $group, "name" => $name);
}
user2250471
  • 1,002
  • 3
  • 10
  • 23

1 Answers1

-1

Here's a pseudocode (since I don't program in PHP for a long time now).

define an empty map
foreach(entry in array) map[entry["group"] + entry["name"]] = entry
map.values
src091
  • 2,807
  • 7
  • 44
  • 74