3

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:

asked May 19, 2011 at 10:22
0

4 Answers 4

9
$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));
answered May 19, 2011 at 10:29
Sign up to request clarification or add additional context in comments.

5 Comments

Your keys are going to wind up being the array indexes, not the individual associative field names.
I think the op only cares about a single field, not 'everything except id'. Also... not to cast dispersions here... but does array_unique work that way? I thought it basically used a strict equality internally, which would preclude its usage for comparing arrays of this nature.
Ok, I looked it up. It appears to use string equality for checking. Which doesn't quite make sense in this context, until I read this: Note: Note that array_unique() is not intended to work on multi dimensional arrays. -- This is an array of associative arrays. Any which way, the use of array_unique scares me. : )
Ok good. It works, but the array keys are not indexed properly which I need to use the array in a for loop, the keys are skipped like [0], [12], [18] etc.
Because of it got the unique arrays by their keys. If you want to renumerate the array just use: $output = array_values($output)
6
$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 an array based on the result of a callback (I used an anonymous function which can be used since PHP 5.3). The time_start values are collected into a temporary array.

answered May 19, 2011 at 10:42

1 Comment

One thing to corrent. array_filter returns filtered array and doesn't change array at place (by refference). More info here
1

I think you'll just have to walk it:

$usedVals = array();
$outArray = array();
foreach ($targetArray as $arrayItem)
{
 if (!in_array($arrayItem['time_start'],$usedVals))
 {
 $outArray[] = $arrayItem;
 $usedVals[] = $arrayItem['time_start'];
 }
}
return $outArray;
answered May 19, 2011 at 10:30

1 Comment

@Darzan As opposed to what? : ) It'll eat more memory than say, the suggestion above... but it really shouldn't be much slower. Plus, there are some logic flaws there that I was about to address.
0
$uniq = array();
foreach($no_unique as $k=>$v) if(!isset($uniq[$v['time_start']])) $uniq[$v['time_start']] = $v;
$uniq = array_values($uniq);
answered May 19, 2011 at 10:30

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.