Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to sort a multidimensional object, and, after looking on php.net and around here, I get that I should write a function that I can then call via usort. I'm having some trouble with the syntax. I haven't ever written something this complicated before, and trying to figure it out feels like a mindbender...

I'm working with the array posted at the end of this post. I want to filter out duplicate [n] values. But, and this is the tricky part for me, I want to keep the [n] value that has the smallest [d] value.

So, if I have (and this example is simplified, the real array is at the end of this post):

Array ( 

[7777] => Array 
            ( [0] => Array
                ( [n] => '12345' [d] => 1 ) 
              [1] => Array
                ( [n] => '67890' [d] => 4 )
            )

[8888] => Array
            ( [2] => Array
                ( [n] => '12345' [d] => 10 )
              [3] => Array
                ( [n] => '67890' [d] => 2 )
            )
)

I want to filter out duplicate [n] values based on the [d] value, so that I wind up with this:

Array ( 

[7777] => Array 
            ( [0] => Array
                ( [n] => '12345' [d] => 1 ) 
            )

[8888] => Array
              [3] => Array
                ( [n] => '67890' [d] => 2 )
            )
)

I've tried writing different variations of the function cmp example posted on php.net, but I haven't been able to get any to work, and I think it's because I'm not altogether clear on how to traverse it using their example...

I tried:

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

But, that really did not work at all... Anyway, here's the real array I'm trying to work with... Help is greatly appreciated!

Array
(
    [32112] => Array
        (
            [0] => Array
                (
                    [n] => '02124'
                    [d] => '0'
                )

            [1] => Array
                (
                    [n] => '02124'
                    [d] => '0.240101905123744'
                )

            [2] => Array
                (
                    [n] => '11050'
                    [d] => '0.441758632682761'
                )

            [3] => Array
                (
                    [n] => '02186'
                    [d] => '0.317514080260304'
                )
        )
    [43434] => Array
        (
            [4] => Array
                (
                    [n] => '02124'
                    [d] => '5.89936971664429e-05'
                )

            [5] => Array
                (
                    [n] => '02124'
                    [d] => '0.145859264792549'
                )

            [6] => Array
                (
                    [n] => '11050'
                    [d] => '0.327864593457739'
                )

            [7] => Array
                (
                    [n] => '11050'
                    [d] => '0.312135345168295'
                )
          )
)
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You're not really sorting as much as filtering. You'll want to for through the arrays storing n/d values and comparing then unsetting when applicable.

share|improve this answer
    
Hmm, well I tried this, but it didn't work, so maybe I'm not understanding or maybe my syntax is wrong? $m = 1; $v = 0; foreach($ns as $key => $value) { foreach($value as $k2) { if($value[$m]['n'] == $value[$v]['n'] && $value[$m]['d'] > $value[$v]['d']) { unset($ns[$key][$m]); } $m++; $v++; } } –  BigDogsBarking Apr 26 '10 at 15:09
    
use for, not foreach. –  webbiedave Apr 26 '10 at 16:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.