Jump to content

Sorting multi-dimensional array


denhamd2

Recommended Posts

I have a muli-dimensional array (see below).

 

I'm trying to use PHP to group matching events. Basically any matchname that is a duplicate (eg. Reds vs Whites) to group them together and group their linksets...

Array
(
    [matches] => Array
        (
            [match] => Array
                (
                    [sportname] => Football
                    [tournamentname] => Crown League
                    [thetime] => 201308021600
                    [matchname] => Reds vs Whites
                            [linkset] => Array
                                (
                                    [link] => link1.html
                                ) 
                    [0] => Array
                        (
                            [sportname] => Football
                            [tournamentname] => Prince League
                            [thetime] => 201308021130
                            [matchname] => Blues vs Yellows
                            [linkset] => Array
                                (
                                    [link] => link2.html
                                ) 
                        )

                    [1] => Array
                        (
                            [sportname] => Football
                            [tournamentname] => Crown League
                            [thetime] => 201308021000
                            [matchname] => Reds vs Whites
                            [linkset] => Array
                                (
                                    [link] => link3.html
                                ) 
                       )
  )
 )
)

I am trying to end up with

Football: Reds vs Whites (201308021600) - Crown League
Links: link1.html, link3.html

 

Football: Blues vs Yellows (201308021130) - Prince League
Links: link2.html

 

I've been looking at the PHP manual but tearing my hair out trying to get it to output the above. Any ideas would be most welcome.

 

Thanks

Edited by denhamd2
Link to comment
Share on other sites

like this

[matches] => Array
        (
            [0] => Array
                (
                    [sportname] => Football
                    [tournamentname] => Crown League
                    [thetime] => 201308021600
                    [matchname] => Reds vs Whites
                    [linkset] => Array
                        (
                            [link] => link1.html
                        )
                ) 
            [1] => Array
                (
                    [sportname] => Football
                    [tournamentname] => Prince League
                    [thetime] => 201308021130
                    [matchname] => Blues vs Yellows
                    [linkset] => Array
                        (
                            [link] => link2.html
                        ) 
                )

            [2] => Array
                (
                    [sportname] => Football
                    [tournamentname] => Crown League
                    [thetime] => 201308021000
                    [matchname] => Reds vs Whites
                    [linkset] => Array
                        (
                            [link] => link3.html
                        ) 
               )
        )


Link to comment
Share on other sites

The matchsort() custom sort function will sort the array by sportname - tournamentname - matchname.

$matches = Array
        (
            0 => Array
                (
                    'sportname' => 'Football',
                    'tournamentname' => 'Crown League',
                    'thetime' => 201308021600,
                    'matchname' => 'Reds vs Whites',
                    'linkset' => Array
                        (
                            'link' => 'link1.html'
                        )
                ) ,
            1 => Array
                (
                    'sportname' => 'Football',
                    'tournamentname' => 'Prince League',
                    'thetime' => 201308021130,
                    'matchname' => 'Blues vs Yellows',
                    'linkset' => Array
                        (
                            'link' => 'link2.html'
                        ) 
                ),

            2 => Array
                (
                    'sportname' => 'Football',
                    'tournamentname' => 'Crown League',
                    'thetime' => 201308021000,
                    'matchname' => 'Greys vs Whites',
                    'linkset' => Array
                        (
                            'link' => 'link3.html'
                        ) 
               )
        );

usort($matches, 'matchsort');        
echo '<pre>',print_r($matches, true),'</pre>';

function matchsort ($a, $b)
{
    $x = strcmp($a['sportname'], $b['sportname']);
    if ($x==0) {
        $y = strcmp($a['tournamentname'], $b['tournamentname']);
        if ($y==0) {
            return strcmp($a['matchname'], $b['matchname']);
        } else{
            return $y;
        }
    } else {
        return $x;
    }
}

OUTPUT

Array
(
    [0] => Array
        (
            [sportname] => Football
            [tournamentname] => Crown League
            [thetime] => 201308021000
            [matchname] => Greys vs Whites
            [linkset] => Array
                (
                    [link] => link3.html
                )

        )

    [1] => Array
        (
            [sportname] => Football
            [tournamentname] => Crown League
            [thetime] => 201308021600
            [matchname] => Reds vs Whites
            [linkset] => Array
                (
                    [link] => link1.html
                )

        )

    [2] => Array
        (
            [sportname] => Football
            [tournamentname] => Prince League
            [thetime] => 201308021130
            [matchname] => Blues vs Yellows
            [linkset] => Array
                (
                    [link] => link2.html
                )

        )

)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.