In this post, I am going to explain how to remove duplicate values from multidimensional array in PHP. I use array_unique()
to get unique but its work on single-dimensional array, not able to work on multidimensional arrays.
You can remove duplicates from the multidimensional array by value in PHP.
I found answers to find a unique multidimensional array after a lot of googling. As a result, we’ll use array serialization for this. Serialization is a technique for storing and transmitting PHP values without compromising their type or structure.
Checkout other tutorial of PHP Array,
- Merge Two Array or Multiple Array in PHP
- How To Convert XML To Associative Array in PHP
- How To Convert XSD into Array Using PHP
- Convert XML to Array in PHP Using XML2Array
- PHP Array Length Tutorial With Example
- How To Add Elements to an Array in PHP?
- Remove Duplicates From Multidimensional Array
array_unique in PHP
The array_unique() function are used to remove duplicate data from given array.
Syntax:array_unique(array, sorttype)
There are two parameters that will be passed in the array_unique()
, first specifying an array that is required and the second is optional specifying the sorting type.
Optional parameters have the following possible values :
- SORT_STRING – Default
- SORT_NUMERIC
- SORT_REGULAR
- SORT_LOCALE_STRING
Remove duplicate values from an array in PHP
Let’s remove duplicate values from an array using PHP array_unique()
method.
<?php $reqArray=array("name"=-->"adam","sub"=>"english","sub"=>"french","sub"=>"english"); echo "<pre>"; print_r(array_unique($reqArray)); ?>
The output of array before removing duplicate values from a multi-dimensional array in PHP.
Array ( [name] => adam [sub] => english )
PHP Multidimensional Array
$reqArray = array( array( "name" => "john" ), array( "age" => "12" ), array( "address" => "Delhi" ), array( "name" => "john" ) );
Remove Duplicate Values from Multidimensional Array:
We will follow the following steps to create a PHP function that will remove duplicate values from the multi dimensional array.
Step 1: First we will use serialize() funtion to serialize the array. Then use the map with PHP inbuilt function.
Step 2: use unserialize()
function to make the serialized string into a PHP value.
Function to remove duplicated values
$reqArray=array(array( "name" => "john" ), array( "age" => "12" ), array( "address" => "Delhi" ), array( "name" => "john" ) ); $output = array_map("unserialize", array_unique(array_map("serialize", $reqArray))); echo "<pre>";print_r($output); }
The output of the array after removing duplicate values from a multi-dimensional array in PHP.
Output:
Array ( [0] => Array ( [name] => john ) [1] => Array ( [age] => 12 ) [2] => Array ( [address] => Delhi ) )
Example 2
Remove Duplicate values From Multi Array
$reqArray=array( array("name","age"), array("depart","salary"), array("name","empiId"), array("name","age") ); $output = array_map("unserialize", array_unique(array_map("serialize", $reqArray))); echo "<pre>";print_r($output);
The output of array after removing duplicate values from a multi-dimensional array in PHP.
Output:
Array ( [0] => Array ( [0] => name [1] => age ) [1] => Array ( [0] => depart [1] => salary ) [2] => Array ( [0] => name [1] => empiId ) )
Find the Duplicate values in array using PHP
<?php $array=array("john","12","john", "13", "12"); $count_array = array_count_values($array); echo "<pre>"; print_r($count_array); ?>
Output:
Array ( [john] => 2 [12] => 2 [13] => 1 )
Right example it worked for me. thanks