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

Link to comment
https://forums.phpfreaks.com/topic/281747-sorting-multi-dimensional-array/
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
                        ) 
               )
        )


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
                )

        )

)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.