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.

Checkout other tutorial of PHP Array,

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.

Select Code
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);

Leave a Reply

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