check php array for key/value duplicates -
this question has answer here:
my array this:
$sth = array( array("group" => "1", "name" => "john", "whatever" => "a"), array("group" => "1", "name" => "john", "whatever" => "b"), // duplicate want remove array("group" => "1", "name" => "henry", "whatever" => "c"), array("group" => "2", "name" => "john", "whatever" => "d"), array("group" => "2", "name" => "peter", "whatever" => "e") )
and want remove duplicate 1/john
entry, not 2/john
since it's not duplicate. want check 2 values, not "whatever" value, since different. know use in_array
search "john"
, or array_search
check keys, keys don't correlate name, need remove duplicates key/value
exact same. i'm thinking like:
$cleaned_sth = array(); foreach($sth $s) { $group = $s[0]; $name = $s[1]; if( in_array($group => $name, $cleaned_sth ) continue; else $cleaned_sth[] = array("group" => $group, "name" => $name); }
here's pseudocode (since don't program in php long time now).
define empty map foreach(entry in array) map[entry["group"] + entry["name"]] = entry map.values
Comments
Post a Comment