MoFish Posted February 26, 2014 Share Posted February 26, 2014 (edited) Hi, I have some code which is pulling back the some database information. I would like the array to be structured slightly differently and wondered if anyone could help me in achieving this. I would like each game to be displayed with its attributes and players of each game to be inside. Something like the below: array ( [game] ( attributes ( [players] ) ) [game] ( attributes ( [players] ) ) } The structure I am currently getting is as follows. I am aware there are some duplicates of data (not sure why) Array ( [0] => Array ( [0] => 1 [id] => 1 [1] => 2014-02-11 [game_date] => 2014-02-11 [2] => 6:00 [game_time] => 6:00 [3] => DISC [game_location] => Japan [4] => As Many As Possible [type_title] => As Many As Possible [5] => Awaiting Players [status_title] => Awaiting Players ) [players] => Array ( [0] => Array ( [0] => 4 [id] => 4 [1] => 10029 [player_fb_id] => 10029 [2] => 2 [game_id] => 2 ) [1] => Array ( [0] => 3 [id] => 3 [1] => 1027 [player_fb_id] => 1027 [2] => 2 [game_id] => 2 ) ) [1] => Array ( [0] => 2 [id] => 2 [1] => 2014-02-18 [game_date] => 2014-02-18 [2] => 6:00 [game_time] => 6:00 [3] => DISC [game_location] => China [4] => 5v5 [type_title] => 5v5 [5] => Awaiting Players [status_title] => Awaiting Players ) ) The code I am using to create the array is as follows: public function get_games () { $db = new sql(); $sql = "SELECT games.`id` , games.`game_date` , games.`game_time` , games.`game_location` , games_types.`type_title`, games_status.`status_title` FROM games JOIN games_types ON games.`game_type_id` = games_types.`id` JOIN games_status ON games.`game_status_id` = games_status.`id` ORDER BY games.id ASC"; $db->query($sql); if($db->num_rows()){ while($k = $db->fetch_array($db)) { $result[] = $k; $pm = new playersManager(); $players=$pm->get_players($k['id']); $result['players'] = $players; } } echo "<pre>"; print_r($result); echo "</pre>"; return $result; } Edited February 26, 2014 by MoFish Quote Link to comment Share on other sites More sharing options...
WebStyles Posted February 26, 2014 Share Posted February 26, 2014 I'm not quite sure I understand the question correctly (do you want to create an index called 'attributes' inside the game array and put the players in there, or just add the players array as one of the game attributes?). But here's an example, and from this you can figure it our easily. $index = 0; // created a variable to keep track of the index number while($k = $db->fetch_array($db)) { $result[$index]['game'] = $k; // using $index number so I can keep track, and added 'game' $pm = new playersManager(); $players=$pm->get_players($k['id']); $result[$index]['players'] = $players; // dumped players in same index as the game data $index++; // increment index number } Hope this helps Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.