0
數組刪除重複我在PHP這樣的數組:基於某一特定領域
[{"pos"=0,"name"="Tom"},{"pos"=1,"name"="John"},{"pos"=2,"name"="Tom"}]
我想刪除這個數組基於「名稱」一式兩份。換句話說,我希望得到一個這樣的數組:
[{"pos"=0,"name"="Tom"},{"pos"=1,"name"="John"}]
我們怎樣才能做到這一點?
數組刪除重複我在PHP這樣的數組:基於某一特定領域
[{"pos"=0,"name"="Tom"},{"pos"=1,"name"="John"},{"pos"=2,"name"="Tom"}]
我想刪除這個數組基於「名稱」一式兩份。換句話說,我希望得到一個這樣的數組:
[{"pos"=0,"name"="Tom"},{"pos"=1,"name"="John"}]
我們怎樣才能做到這一點?
$unique = array();
$arr = array_filter($arr, function($v) use(&$unique){
$inArray = in_array($v->name, $unique);
if(!$inArray) $unique[] = $v->name;
return !$inArray;
});
http://us3.php.net/function.array-unique – crafter
@crafter祝你好運...... – Emissary