Adamhumbug Posted January 3, 2022 Share Posted January 3, 2022 Hi All, I have a php function that returns a list of players and which team they play for. I am looking to create an array that at the top level has the teams and then second level will be first and last name of the player. i think i have done this like this $stmt -> bind_result($fn, $ln, $team); $squad = []; while($stmt -> fetch()){ $squad[$team] .= [$fn, $ln]; } Then i am wanting to return the output to my page as a list (for ease of the example) with the team at the top and all of the player underneath: Team 1 Player 1 Player 2 Team 2 Player 3 Player 4 foreach($squad as $team){ $out .= "$team"; foreach($team as $key => $val){ $out.="$key $value"; }; }; I thought that the above could help me but i am getting a bit lost. Quote Link to comment https://forums.phpfreaks.com/topic/314383-returning-the-result-of-multidimentional-array/ Share on other sites More sharing options...
Solution Barand Posted January 3, 2022 Solution Share Posted January 3, 2022 (This example uses the "house" and "pupil" tables from my SQL tutorials instead of your teams and players) $res = $db->query("SELECT h.house_name , p.fname , p.lname FROM house h JOIN pupil p USING (houseid) "); $data = []; foreach ($res as $row) { if (!isset($data[$row['house_name']])) { $data[$row['house_name']] = []; } $data[$row['house_name']][] = [$row['fname'], $row['lname']]; } foreach ($data as $house => $pupils) { echo "$house<ul>"; foreach ($pupils as $p) { echo "<li>{$p[0]} {$p[1]}</li>"; } echo "</ul>"; } giving Quote Link to comment https://forums.phpfreaks.com/topic/314383-returning-the-result-of-multidimentional-array/#findComment-1593145 Share on other sites More sharing options...
Adamhumbug Posted January 3, 2022 Author Share Posted January 3, 2022 I do have one little issue with this. I have the following foreach ($res as $row) { if (!isset($data[$row['house_name']])) { $data[$row['house_name']] = []; } $data[$row['name']][] = [$row['id'], $row['first_name'], $row['last_name']]; } foreach ($data as $team => $players) { $out .= "<div class='col-4'> <h5>$team</h5> "; foreach ($players as $p) { $out.= "<input class='form-control w-100 mb-3' type='text' disabled='disabled' name='' data-value='$p[0]' value='$p[1] $p[2]'>"; } $out .= "</div>"; } return $out; and i am getting a rougue div created before the 2 divs that i am expecting - you can see the 3 col-4s The first one is always blank. I am assuming that this is something simple but i am struggling to see what it is. Quote Link to comment https://forums.phpfreaks.com/topic/314383-returning-the-result-of-multidimentional-array/#findComment-1593148 Share on other sites More sharing options...
Barand Posted January 3, 2022 Share Posted January 3, 2022 The code you posted above outputs this for me (changed only by adjusting newlines and tabs) <div class="col-4"> <h5>Laker</h5> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="1" value="Adam Simms"> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="7" value="David Powell"> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="9" value="George Wilson"> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="11" value="Jane Morrison"> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="21" value="Peter Adamson"> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="23" value="Wayne Jones"> </div> <div class="col-4"> <h5>Grace</h5> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="2" value="Allan Blair"> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="5" value="Anthony Bell"> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="6" value="Caroline Freeman"> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="14" value="John Watson"> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="15" value="Jack Williams"> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="19" value="Mary Whitehouse"> </div> <div class="col-4"> <h5>Jardine</h5> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="4" value="Anne Bailey"> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="12" value="John Patterson"> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="13" value="John Tully"> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="18" value="Mary Sheldon"> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="20" value="Michael Grove"> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="22" value="Peter Appleby"> </div> <div class="col-4"> <h5>Cowdrey</h5> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="3" value="Anna Hamilton"> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="8" value="Emma Watson"> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="10" value="Henry Irving"> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="16" value="Margaret Norton"> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="17" value="Mary Blake"> <input class="form-control w-100 mb-3" type="text" disabled="disabled" name="" data-value="24" value="William Smith"> </div> No col-6s and nothing rogue. Quote Link to comment https://forums.phpfreaks.com/topic/314383-returning-the-result-of-multidimentional-array/#findComment-1593149 Share on other sites More sharing options...
Adamhumbug Posted January 4, 2022 Author Share Posted January 4, 2022 I found what it was in my code that was outputting the extra div. I had left something in above that wasnt needed. Thanks for your help as always. Quote Link to comment https://forums.phpfreaks.com/topic/314383-returning-the-result-of-multidimentional-array/#findComment-1593158 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.