php sorting a seriously multidimensional array...

Posted by BigDogsBarking on Stack Overflow See other posts from Stack Overflow or by BigDogsBarking
Published on 2010-04-26T06:36:45Z Indexed on 2010/04/26 6:43 UTC
Read the original article Hit count: 302

Filed under:
|
|
|

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'
                )
          )
)

© Stack Overflow or respective owner

Related posts about php

Related posts about multidimensional