views:

47

answers:

3

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;
}
A: 

Use array_unique and sort

Chris
+2  A: 

In your comparison function $a and $b are both items of your array. To sort the items by num, use this:

function cmp($a, $b) {
    if ($a['num'] == $b['num']) {
        return 0;
    }
    return ($a['num'] < $b['num']) ? -1 : 1;
}

And to sort by num and then by dis, use this:

function cmp($a, $b) {
    if ($a['num'] == $b['num']) {
        if ($a['dis'] == $b['dis']) {
            return 0;
        }
        return ($a['dis'] < $b['dis']) ? -1 : 1;
    }
    return ($a['num'] < $b['num']) ? -1 : 1;
}

After sorting your array you can filter the items with duplicate num with this:

for ($i=1, $j=0, $n=count($array); $i<$n; ++$i) {
    if ($array[$i]['num'] == $array[$j]['num']) {
        unset($array[$i]);
    } else {
        $j = $i;
    }
}

And everything together:

$array = array(
    array('num' => '09989', 'dis' => '20'),
    array('num' => '09989', 'dis' => '10'),
    array('num' => '56676', 'dis' => '15'),
    array('num' => '44533', 'dis' => '20'),
    array('num' => '44533', 'dis' => '50')
);

function cmp($a, $b) {
    if ($a['num'] == $b['num']) {
        if ($a['dis'] == $b['dis']) {
            return 0;
        }
        return ($a['dis'] < $b['dis']) ? -1 : 1;
    }
    return ($a['num'] < $b['num']) ? -1 : 1;
}
usort($array, 'cmp');

for ($i=1, $j=0, $n=count($array); $i<$n; ++$i) {
    if ($array[$i]['num'] == $array[$j]['num']) {
        unset($array[$i]);
    } else {
        $j = $i;
    }
}
var_dump($array);
Gumbo
Trying to use this but I just get an Invalid comparison function error
c41122ino
@c41122ino: What do you mean by you get an invalid comparison function? Does PHP say that?
Gumbo
+1  A: 

For sort:

    __retry:
    for ($j=1; $j < sizeof($your_array[$i]); $j++)
    {
        if (cmp($your_array[$i][$j-1],$your_array[$i][$j])) // your written cmp for your object structure
        {
            $temp = $your_array[$i][$j-1];
            $your_array[$i][$j-1] = $your_array[$i][$j];
            $your_array[$i][$j] = $temp;
            goto __retry;
        }
    }

Your's compare function:

function cmp($a, $b)
{
    return $a['num'] < $b['num'];
}

For delete same:

    __retry:
    for (%j=1; $j < sizeof($your_array[$i]); $j++)
    {
        if (!cmp($your_array[$i][$j-1],$your_array[$i][$j]) && !cmp($your_array[$i][$j],$your_array[$i][$j-1]))
        {
            $temp = $your_array[$i][$j-1] = array_pop($your_array[$i]);
        }
    }
Svisstack
Why not use `sort` for sorting the array? At present your code is, to be brutal, fairly unreadable (and I am not sure that it works).
Yacoby
Well, trying to implement what you posted here yielded: Parse error: syntax error, unexpected ':' It's not liking that __retry: ...
c41122ino
delete __retry.replace goto __retry; to $j = 1
Svisstack