A multidimensional array unique in value (not an array)

I have a multidimensional array that I need to sort with uniqueness, since I have duplicate entries, so I need array_unique to go through the array and remove duplicates by value, for example.

 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 duplicates of time_start , which they are all the same, also level and input_level , but they should not be affected, only if there is a corresponding time_start , it should delete it and process the entire array (the array is larger than you think, but I just posted a small example of the array ) You should remove the cheats and return as follows:

 Array ( [0] => Array ( [id] => 324 [time_start] => 1301612580 [level] => 0.002 [input_level] => 0.002 ) ) 

Questions I found that do not work:

  • reformat multidimensional array based on value
  • Remove item from multidimensional array based on value
+3
arrays php multidimensional-array
source share
4 answers
 $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)); 
+8
source share
 $temp = array(); array_filter($yourArray, function ($v) use (&$temp) { if (in_array($v['time_start'], $temp)) { return false; } else { array_push($temp, $v['time_start']); return true; } }); 

Uses array_filter() , which will filter the array based on the result of the callback (I used an anonymous function that can be used with PHP 5.3). The time_start values ​​are collected in a temporary array.

+5
source share

I think you just need to go through:

 $usedVals = array(); $outArray = array(); foreach ($targetArray as $arrayItem) { if (!in_array($arrayItem['time_start'],$usedVals)) { $outArray[] = $arrayItem; $usedVals[] = $arrayItem['time_start']; } } return $outArray; 
+1
source share
 $uniq = array(); foreach($no_unique as $k=>$v) if(!isset($uniq[$v['time_start']])) $uniq[$v['time_start']] = $v; $uniq = array_values($uniq); 
0
source share

All Articles