Multidimensional array unique based on value (not array key)

arrays, multidimensional-array, php

Solution

$input = array( /* your data */ );
$temp  = array();
$keys  = array();

foreach ( $input as $key => $data ) {
    unset($data['id']);
    if ( !in_array($data, $temp) ) {
        $temp[]     = $data;
        $keys[$key] = true;
    }
}

$output = array_intersect_key($input, $keys);

or

$input = array( /* your data */ );
$temp  = $input;

foreach ( $temp as &$data ) {
    unset($data['id']);
}

$output = array_intersect_key($input, array_unique($temp));

Problem

I have a multidimensional array which I need to be sorted with uniqueness as I have duplicated records, so I need `array_unique` to go through the array and remove duplicates by the value, e.g. ``` Array ( [0] => Array ( [id] => 324 [time_start] => 1301612580 [level] => 0.002 [input_level] => 0.002 ) [1] => Array ( [id] => 325 [time_start] => 1301612580 [level] => 0.002 [input_level] => 0.002 ) [2] => Array ( [id] => 326 [time_start] => 1301612580 [level] => 0.002 [input_level] => 0.002 ) ) ``` There are duplicated `time_start`, which they are all the same, also `level` and `input_level` but they are not to be affected, only if there are matching `time_start` it should remove it and process the whole array (the array is bigger than you think, but I just posted a small example of the array). Should remove dupes and return like this: ``` Array ( [0] => Array ( [id] => 324 [time_start] => 1301612580 [level] => 0.002 [input_level] => 0.002 ) ) ``` Questions I've found that didn't work: - reformat multidimensional array based on value - Delete element from multidimensional-array based on value

Original source

Related problems