php - sort and delete duplicates?
Posted
by c41122ino
on Stack Overflow
See other posts from Stack Overflow
or by c41122ino
Published on 2010-04-18T22:18:00Z
Indexed on
2010/04/18
22:23 UTC
Read the original article
Hit count: 350
I've got an array that looks like this:
Array (
[0] => Array (
num => 09989,
dis => 20
)
[1] => Array (
num => 09989,
dis => 10
)
[2] => Array (
num => 56676,
dis => 15
)
[3] => Array (
num => 44533,
dis => 20
)
[4] => Array (
num => 44533,
dis => 50
)
)
First, I'm trying to sort them by num, and can't seem to get the usort example from php.net working here. It simply doesn't appear to be sorting... I'm also trying to delete the array element if it's a duplicate and whose dis value is higher than the other one.
So, based on the example above, I'm trying to create:
Array (
[0] => Array (
num => 09989,
dis => 10
)
[1] => Array (
num => 44533,
dis => 20
)
[2] => Array (
num => 56676,
dis => 15
)
)
This is the code from php.net:
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
© Stack Overflow or respective owner