Multi-Dimensional Array Removing Duplicates

Hi,

I am testing an example of removing duplicates from an array. What i want to do is check to see which has a lower ‘Value’ and if it has a duplicate then remove the one with a lower value.

So for example, with this code i am trying out:


            $items = array
      (
          'x' => array
              (
                  'y' => array
                      (
                          0 => array
                              (
                                  'ID' => 390517,
                                  'Name' => 'Bill',
                                  'Value' => 80
                                  ),
                          1 => array
                              (
                                  'ID' => 390519,
                                  'Name' => 'Mike',
                                  'Value' => 85
                                  ),
                          2 => array
                              (
                                  'ID' => 390533,
                                  'Name' => 'Sam',
                                  'Value' => 55
                                  ),
                          3 => array
                              (
                                  'ID' => 390519,
                                  'Name' => 'Mike',
                                  'Value' => 95
                                  )
                     )
              )
      );

/**
* remove duplicates of higher value
*/
$a = $items['x']['y'];
$k = count($a);
for($i=0; $i < $k-1; $i++)
{
    for($j=$i+1; $j < $k; $j++)
    {
        if ($a[$i] == $a[$j]) unset($a[$j]);
    }
}
$items['x']['y'] = $a;
echo '<pre>', print_r($items, true), '</pre>';

The array with an ID of 390519 and a value of 85 should be removed. But that code does not work, it prints out the full array.

Any ideas how i can do this?

Thanks

Thanks :slight_smile:

http://php.net/manual/en/function.array-unique.php