PHP Removing duplicates from a multidimensional array based on a key

I have a multidimensional array ($ array) in which the entries look:

{ ["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" } } 

I need to remove any duplicate values ​​in $array based on key. Therefore, if my array was:

 $array = array( ["upload/example.gif"] => etc.... ["upload/tree.gif"] => etc.... ["upload/example.gif"] => etc....) 

I could remove one of the arrays ["upload/example.gif"] => etc....

I tried:

 $array = array_map('unserialize', array_unique(array_map('serialize', $array))); 

but it didn’t work.

Thanks in advance.

+4
source share
1 answer

netcoder answered the question correctly, I just wanted it to appear in the answer window (instead of the comment).

You cannot have duplicate keys in an associative array (or any array). By its nature, it is an array that can be guaranteed with unique keys.

BTW, if you find duplicate data in different keys, you can delete the redundant key by removing it.

 unset($array['delete_me']); 

http://php.net/manual/en/function.unset.php

0
source

Source: https://habr.com/ru/post/1347568/


All Articles