Remove duplicates from multidimensional array
As we know array_unique() removes duplicate values from an array. but array_unique() is not intended to work on multi dimensional arrays. So what to do when it comes to multi dimensional array.
We have to serialize the array with serialize() then run the array_unique() function. Serialization is useful for storing or passing PHP values around without losing their type and structure.
We use unserialize() to make the serialized string into a PHP value again. array_map() will be a handy function in this case.
You can read more about this useful function here.
Using all above four functions we get a little but useful function to remove duplicates from multi dimensional array as follows.
We have to serialize the array with serialize() then run the array_unique() function. Serialization is useful for storing or passing PHP values around without losing their type and structure.
We use unserialize() to make the serialized string into a PHP value again. array_map() will be a handy function in this case.
You can read more about this useful function here.
Using all above four functions we get a little but useful function to remove duplicates from multi dimensional array as follows.
function multi_array_unique($src){ $output = array_map("unserialize", array_unique(array_map("serialize", $src))); return $output; } //Call function $src=array( array("apple","banana"), array("banana","guava"), array("apple","banana"), array("banana","guava") ); $out=multi_array_unique($src);
Remove duplicates from multidimensional array
Reviewed by JS Pixels
on
July 14, 2012
Rating:
thanks for this code its very helpful..
ReplyDeletethanks for the code..,it helps me lot
ReplyDeleteThis is a useful way to do this. What do you do in the case of the multi-dimensional array that contains arrays and string or integers?
ReplyDeleteArray
ReplyDelete(
[155] => Array
(
[0] => Array
(
[TokenId] => 34b082e722f45d317638402b94e22642
[user_id] => 155
[Name] => Pradepe
[email_address] => pradeep123@gmail.com
[facebook_email] => pradeep0@gmail.com
[google_email] => abc330@gmail.com
[picture] => asdsa asdasd asd
[user_type_id] => sadasdasdasdasdasd
)
)
[154] => Array
(
[0] => Array
(
[TokenId] => 34b082e722f45d317638402b94e22642
[user_id] => 154
[Name] => Pradepe
[email_address] =>
[facebook_email] =>
[google_email] => p3300@gmail.com
[picture] => asdsa asdasd asd
[user_type_id] => sadasdasdasdasdasd
)
[1] => Array
(
[TokenId] => 34b082e722f45d317638402b94e22642
[user_id] => 154
[Name] => Pradepe
[email_address] =>
[facebook_email] =>
[google_email] => p3300@gmail.com
[picture] => asdsa asdasd asd
[user_type_id] => sadasdasdasdasdasd
)
[2] => Array
(
[TokenId] => 34b082e722f45d317638402b94e22642
[user_id] => 154
[Name] => Rahul
[email_address] =>
[facebook_email] =>
[google_email] => p3300@gmail.com
[picture] => asdsa asdasd asd
[user_type_id] => sadasdasdasdasdasd
)
)
[174] => Array
(
[0] => Array
(
[TokenId] => 34b082e722f45d317638402b94e22642
[user_id] => 174
[Name] => Rahul
[email_address] =>
[facebook_email] =>
[google_email] => pr00@gmail.com
[picture] => asdsa asdasd asd
[user_type_id] => sadasdasdasdasdasd
)
)
)
How to remove duplicate google email ids from the array
Thanks
ReplyDelete