- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove duplicated elements of associative array in PHP
The ‘array_map’ function sends the value of every element in the array to a user-defined function. It then returns an array with new values, because of calling the user-defined function on the array.
Syntax of array_map function
array_map ( user-defined function, array_1, array_2, array_3…)
The user-defined function and the array_1 are compulsory arguments, but array_2 and array_3 are optional.
Example
$result = array( 0=>array('a'=>1,'b'=>'Hello'), 1=>array('a'=>1,'b'=>'duplicate_val'), 2=>array('a'=>1,'b'=>'duplicate_val') ); $unique = array_map("unserialize", array_unique(array_map("serialize", $result))); print_r($unique);
Output
This will produce the following output −
Array ( [0] => Array ( [a] => 1 [b] => Hello ) [1] => Array ( [a] => 1 [b] => duplicate_val ) )
In the above code, an array is defined with 3 elements and this is assigned to a variable named ‘result’. The array_map function is called and the ‘result’ value is passed as a parameter.
The resultant output would be the contents in the ‘result’ variable along with mention about the duplicate value in the array.
- Related Articles
- PHP Associative Array
- Convert an object to associative array in PHP
- How to get numeric index of associative array in PHP?
- PHP array_push() to create an associative array?
- PHP Pushing values into an associative array?
- How to build dynamic associative array from simple array in php?
- Associative Arrays in PHP
- PHP program to add item at the beginning of associative array
- How to access an associative array by integer index in PHP?
- How to remove duplicates from the sorted array and return the non-duplicated array using C#?
- PHP: Remove object from array
- Length of a JavaScript associative array?
- Removing empty array elements in PHP
- Reset keys of array elements using PHP ?
- Find lost element from a duplicated array in Python

Advertisements