1

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

4 Answers 4

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Trying to use this but I just get an Invalid comparison function error
@c41122ino: What do you mean by you get an invalid comparison function? Does PHP say that?
1

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]);
        }
    }

3 Comments

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).
Well, trying to implement what you posted here yielded: Parse error: syntax error, unexpected ':' It's not liking that __retry: ...
delete __retry. replace goto __retry; to $j = 1
0

Use array_unique() and sort()

Comments

0
$firstArray=array(5,6,7,7,1,6,1,5);

//sort the first array
sort($firstArray);
print_r($firstArray);

//get the number of elements
$arrayCount=count($firstArray);

//create a second array
$secondArray=array();

//copy the first item to the second array
array_push($secondArray,$firstArray[0]);    

//copy into the second array if there is not twice the same item
for ($j=1;$j<$arrayCount;$j++)
{
    if($firstArray[$j-1]!=$firstArray[$j]) array_push($secondArray,$firstArray[$j]);            
}

//that's all!
print_r($secondArray);

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.