Jump to content

re-structure an array


poe

Recommended Posts

how do i re-structure my array from:
[code]
$leagueAry = array(
array( 'id' => '19', 'team' => 'islanders', 'conf' => 'eastern', 'div' => 'atlantic', ),
array( 'id' => '18', 'team' => 'devils', 'conf' => 'eastern', 'div' => 'atlantic', ),
array( 'id' => '20', 'team' => 'rangers', 'conf' => 'eastern', 'div' => 'atlantic', ),
array( 'id' => '24', 'team' => 'penguins', 'conf' => 'eastern', 'div' => 'atlantic', ),
array( 'id' => '22', 'team' => 'flyers', 'conf' => 'eastern', 'div' => 'atlantic', ),
array( 'id' => '4', 'team' => 'sabres', 'conf' => 'eastern', 'div' => 'northeast', ),
array( 'id' => '16', 'team' => 'canadiens', 'conf' => 'eastern', 'div' => 'northeast', ),
array( 'id' => '28', 'team' => 'maple leafs', 'conf' => 'eastern', 'div' => 'northeast', ),
array( 'id' => '21', 'team' => 'senators', 'conf' => 'eastern', 'div' => 'northeast', ),
array( 'id' => '3', 'team' => 'bruins', 'conf' => 'eastern', 'div' => 'northeast', ),
array( 'id' => '2', 'team' => 'thrashers', 'conf' => 'eastern', 'div' => 'southeast', ),
array( 'id' => '6', 'team' => 'hurricanes', 'conf' => 'eastern', 'div' => 'southeast', ),
array( 'id' => '27', 'team' => 'lightning', 'conf' => 'eastern', 'div' => 'southeast', ),
array( 'id' => '30', 'team' => 'capitals', 'conf' => 'eastern', 'div' => 'southeast', ),
array( 'id' => '13', 'team' => 'panthers', 'conf' => 'eastern', 'div' => 'southeast', ),
array( 'id' => '17', 'team' => 'predators', 'conf' => 'western', 'div' => 'central', ),
array( 'id' => '11', 'team' => 'red wings', 'conf' => 'western', 'div' => 'central', ),
array( 'id' => '7', 'team' => 'blackhawks', 'conf' => 'western', 'div' => 'central', ),
array( 'id' => '26', 'team' => 'blues', 'conf' => 'western', 'div' => 'central', ),
array( 'id' => '9', 'team' => 'blue jackets', 'conf' => 'western', 'div' => 'central', ),
array( 'id' => '12', 'team' => 'oilers', 'conf' => 'western', 'div' => 'northwest', ),
array( 'id' => '15', 'team' => 'wild', 'conf' => 'western', 'div' => 'northwest', ),
array( 'id' => '29', 'team' => 'canucks', 'conf' => 'western', 'div' => 'northwest', ),
array( 'id' => '5', 'team' => 'flames', 'conf' => 'western', 'div' => 'northwest', ),
array( 'id' => '8', 'team' => 'avalanche', 'conf' => 'western', 'div' => 'northwest', ),
array( 'id' => '1', 'team' => 'ducks', 'conf' => 'western', 'div' => 'pacific', ),
array( 'id' => '25', 'team' => 'sharks', 'conf' => 'western', 'div' => 'pacific', ),
array( 'id' => '10', 'team' => 'stars', 'conf' => 'western', 'div' => 'pacific', ),
array( 'id' => '14', 'team' => 'kings', 'conf' => 'western', 'div' => 'pacific', ),
array( 'id' => '23', 'team' => 'coyotes', 'conf' => 'western', 'div' => 'pacific', ),
);
[/code]

to:
[code]
$leagueAry = array(
'eastern' => array(
'atlantic' => array(
'0' => array( 'id' => '19', 'team' => 'islanders', ),
'1' => array( 'id' => '18', 'team' => 'devils', ),
'2' => array( 'id' => '20', 'team' => 'rangers', ),
'3' => array( 'id' => '24', 'team' => 'penguins', ),
'4' => array( 'id' => '22', 'team' => 'flyers', ),
),
'northeast' => array(
'0' => array( 'id' => '4', 'team' => 'sabres', ),
'1' => array( 'id' => '16', 'team' => 'canadiens', ),
'2' => array( 'id' => '28', 'team' => 'maple leafs', ),
'3' => array( 'id' => '21', 'team' => 'senators', ),
'4' => array( 'id' => '3', 'team' => 'bruins', ),
),
'southeast' => array(
'0' => array( 'id' => '2', 'team' => 'thrashers', ),
'1' => array( 'id' => '6', 'team' => 'hurricanes', ),
'2' => array( 'id' => '27', 'team' => 'lightning', ),
'3' => array( 'id' => '30', 'team' => 'capitals', ),
'4' => array( 'id' => '13', 'team' => 'panthers', ),
),
),
'western' => array(
'central' => array(
'0' => array( 'id' => '17', 'team' => 'predators', ),
'1' => array( 'id' => '11', 'team' => 'red wings', ),
'2' => array( 'id' => '7', 'team' => 'blackhawks', ),
'3' => array( 'id' => '26', 'team' => 'blues', ),
'4' => array( 'id' => '9', 'team' => 'blue jackets', ),
),
'northwest' => array(
'0' => array( 'id' => '12', 'team' => 'oilers', ),
'1' => array( 'id' => '15', 'team' => 'wild', ),
'2' => array( 'id' => '29', 'team' => 'canucks', ),
'3' => array( 'id' => '5', 'team' => 'flames', ),
'4' => array( 'id' => '8', 'team' => 'avalanche', ),
),
'pacific' => array(
'0' => array( 'id' => '1', 'team' => 'ducks', ),
'1' => array( 'id' => '25', 'team' => 'sharks', ),
'2' => array( 'id' => '10', 'team' => 'stars', ),
'3' => array( 'id' => '14', 'team' => 'kings', ),
'4' => array( 'id' => '23', 'team' => 'coyotes', ),
),
),
);

[/code]

i tried:
[code]
foreach($leagueAry as $k => $v) {
$teamAry[] = array(
$leagueAry[$k][conf] => array(
$leagueAry[$k][div] => array(
array(
'id' => $teams[$k][id],
'team' => $teams[$k][team],
),
),
),
);
}
[/code]

but, the result is quite not what i want:
[code]

Array
(
    [0] => Array(
            [eastern] => Array(
                    [atlantic] => Array(
                            [0] => Array(
                                    [id] => 19
                                    [team] => islanders
                                )

                        )

                )

        )

    [1] => Array(
            [eastern] => Array(
                    [atlantic] => Array(
                            [0] => Array(
                                    [id] => 18
                                    [team] => devils
                                )

                        )

                )

        )

    [2] => Array(
            [eastern] => Array(
                    [atlantic] => Array(
                            [0] => Array(
                                    [id] => 20
                                    [team] => rangers
                                )

                        )

                )

        )

    [3] => Array(
            [eastern] => Array(
                    [atlantic] => Array(
                            [0] => Array(
                                    [id] => 24
                                    [team] => penguins
                                )

                        )

                )

        )

    [4] => Array(
            [eastern] => Array(
                    [atlantic] => Array(
                            [0] => Array(
                                    [id] => 22
                                    [team] => flyers
                                )

                        )

                )

        )

    [5] => Array(
            [eastern] => Array(
                    [northeast] => Array(
                            [0] => Array(
                                    [id] => 4
                                    [team] => sabres
                                )

                        )

                )

        )

    [6] => Array(
            [eastern] => Array(
                    [northeast] => Array(
                            [0] => Array(
                                    [id] => 16
                                    [team] => canadiens
                                )

                        )

                )

        )

    [7] => Array(
            [eastern] => Array(
                    [northeast] => Array(
                            [0] => Array(
                                    [id] => 28
                                    [team] => maple leafs
                                )

                        )

                )

        )

etc .......
[/code]
Link to comment
https://forums.phpfreaks.com/topic/29589-re-structure-an-array/
Share on other sites

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.