Remove Duplicates From Multidimensional Array

Today i am working on multidimensional array.I need unique array of multidimensional array. I use array_unique() to get unique but its work on single dimensional array not to work on multi dimensional arrays. After alot of goggling I have found answer to find unique multidimensional array,so for this We will use serialization of array. Serialization is used to storing or passing PHP values around without losing their type and structure.

Step to remove duplicate values from multidimensional array:

Step 1:First we will use serialize () funtion to serialize the array. Then use map php inbuilt function.
Step 2: use unserialize() function to make the serialized string into a PHP value.


Function to remove duplicates from multi dimensional array as follows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function multi_unique($src){
     $output = array_map("unserialize",
     array_unique(array_map("serialize", $src)));
   return $output;
}

//Call function

$reqArray=array( array("name","name"), 
            array("age","12"), 
            array("name","parvez"), 
            array("add","delhi")
     );

$output=multi_unique($reqArray);
The following two tabs change content below.
Hi, This is Parvez Alam from India. I am software developer with 4 years’ experience in web development. I have submitted articles on PHP, Mysql, Magento,CSS, HTML, jQuery, web designing and social API. You can subscribe to my blog via RSS/Twitter/Google plus and Facebook. parvez1487(at)gmail(dot)com

Latest posts by Parvez (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>