[英]PHP Remove duplicates from multidimensional array based on key
我有一個多維數組($ array),其中的條目看起來像:
{ ["upload/example.gif"]=> array(5) {
["title"]=> string(12) "This is me"
["excerpt"]=> string(24) "This is a photo of a tree"
["img"]=> string(42) "upload/example.gif"
["link"]=> string(23) "http://www.google.co.uk"
["source"]=> string(6) "custom"
}
}
我需要能夠根據鍵刪除$array
中的所有重復值。 因此,如果我的數組是:
$array = array( ["upload/example.gif"] => etc....
["upload/tree.gif"] => etc....
["upload/example.gif"] => etc....)
我將能夠刪除["upload/example.gif"] => etc....
數組之一。
我試過了:
$array = array_map('unserialize', array_unique(array_map('serialize', $array)));
但這沒用。
提前致謝。
netcoder正確回答了這個問題,我只是希望它顯示在答案框中(而不是評論)。
關聯數組(或任何數組)中不能有重復的鍵。 從本質上講,它是一個數組,您可以保證擁有唯一的鍵。
順便說一句,如果您在不同的密鑰中發現重復的數據,則可以通過取消設置來刪除冗余密鑰。
unset($array['delete_me']);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.