PHP unique array by value?

I have an array in PHP that looks like this:

       array(2) {
              string(9) "My_item"
              string(24) ""
     array(2) {
             string(9) "My_item"
            string(24) ""

The two values in “name” are the same in this two items. I want to sort out duplicates like this.

How do I create an unique array by checking the “name” value?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1


$unique_array = [];
foreach($your_array as $element) {
    $hash = $element[field-that-should-be-unique];
    $unique_array[$hash] = $element;
$result = array_values($unique_array);

Solution 2

Serialisation is very useful for simplifying the process of establishing the uniqueness of a hierarchical array. Use this one liner to retrieve an array containing only unique elements.

$unique = array_map("unserialize", array_unique(array_map("serialize", $input)));

Solution 3

Please find this link useful, uses md5 hash to examine the duplicates:

Quick Glimpse:

 * Create Unique Arrays using an md5 hash
 * @param array $array
 * @return array
function arrayUnique($array, $preserveKeys = false)
    // Unique Array for return
    $arrayRewrite = array();
    // Array with the md5 hashes
    $arrayHashes = array();
    foreach($array as $key => $item) {
        // Serialize the current element and create a md5 hash
        $hash = md5(serialize($item));
        // If the md5 didn't come up yet, add the element to
        // to arrayRewrite, otherwise drop it
        if (!isset($arrayHashes[$hash])) {
            // Save the current element hash
            $arrayHashes[$hash] = $hash;
            // Add element to the unique Array
            if ($preserveKeys) {
                $arrayRewrite[$key] = $item;
            } else {
                $arrayRewrite[] = $item;
    return $arrayRewrite;

$uniqueArray = arrayUnique($array);

See the working example here:

Solution 4

Simple Solution:

 * @param $array
 * @param null $key
 * @return array
public static function unique($array,$key = null){
    if(null === $key){
        return array_unique($array);
    $ret = [];
    foreach($array as $elem){
        $arrayKey = (is_array($elem))?$elem[$key]:$elem->$key;
        $ret[] = $elem;
    return $ret;

Solution 5

function unique_multidim_array($array, $key) { 
            $temp_array = array(); 
            $i = 0; 
            $key_array = array(); 

            foreach($array as $val) { 
                if (!in_array($val[$key], $key_array)) { 
                    $key_array[$i] = $val[$key]; 
                    $temp_array[$i] = $val; 
            return $temp_array; 
$result = unique_multidim_array($visitors,'ip');

Solution 6

Given that the keys on the array (0,1) do not seem to be significant a simple solution would be to use the value of the element referenced by ‘name’ as the key for the outer array:

   array(2) {
          string(9) "My_item"
          string(24) ""

…and if there is only one value other than the ‘name’ why bother with a nested array at all?


Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply