How can you make a multidimensional array unique? [duplicate]

You can use an associative array.

$temp_array = array();
foreach ($array as &$v) {
    if (!isset($temp_array[$v['name']]))
        $temp_array[$v['name']] =& $v;
}

This creates a temporary array, using $v['name'] as the key. If there is already an element with the same key, it is not added to the temporary array.

You can convert the associative array back to a sequential array, using

$array = array_values($temp_array);

Example code and output: http://codepad.org/zHfbtUrl

Leave a Comment