I want to search an array for duplicate values in each subarray and if there is one, only keep the highest value connected to this item.
That's my question, but I love to add an example to clarify things!
This is my array:
Array
(
[0] => Array
(
[groupid] => 1
[points] => 5
)
[1] => Array
(
[groupid] => 1
[points] => 1
)
[2] => Array
(
[groupid] => 1
[points] => 4
)
[3] => Array
(
[groupid] => 2
[points] => 1
)
)
As you can see, the first three items have the same groupid
. I want to match those items and see which of them has the highest points. The other one (with the lowest points) should be kicked out of the array.
So in this case, item 0
, 1
and 2
are from the same group, but 2
has the highest points. 0
and 1
should be dropped out of the array.
My desired result would be something like this:
Array
(
[0] => Array
(
[groupid] => 1
[points] => 5
)
[1] => Array
(
[groupid] => 2
[points] => 1
)
)
I've been trying for a few hours now but no luck yet. Maybe I am overlooking something very simple and thinking to hard to figure this out... Any help would be appreciated!
EDIT It's a Drupal site (but that shouldn't make any difference) and this is query:
foreach ($promotions as $value) {
$promo = db_query("SELECT * FROM {_promo_articles} WHERE art = :art AND promoid = :promoid", array(
':art' => $value['product'][0]->sku,
':promoid' => $value['promo_id'][0])) ->fetchAll();
}