Adamhumbug Posted November 9, 2021 Share Posted November 9, 2021 Hi All, I have been scouring the internet for a good source that shows how to create an array and loop through it to output it to the page. I have done this several times but everytime it comes to doing it i have to scratch my head and google to get anywhere near the answer. I have created a 2 dimentional array of data using the following: function getExistingAwards(){ include 'includes/dbconn.php'; $stmt = $conn -> prepare("SELECT id, award_name, award_year, award_winner from award"); $stmt -> execute(); $stmt -> bind_result($id, $an, $ay, $aw); $awards = array(); $out = ''; while($stmt -> fetch()){ $awards[$ay] = [$id, $an, $aw]; } } This gives me a data structure that looks like this. Array ( [2021] => Array ( [0] => 1 [1] => Avenue Park Award [2] => 1 ) [2019] => Array ( [0] => 2 [1] => Avenue Park Award [2] => 1 ) ) Issue 1 - This looks correct but if there are more than one entries that has the year 2021 for example, only the last one will be added to the array. Issue 2 - After i have the correct data in the array how do i go about looping through each level of the array. I really struggle with for each loops. As always, your help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/314188-multidimentional-arrays-creating-and-looping-for-output/ Share on other sites More sharing options...
Barand Posted November 9, 2021 Share Posted November 9, 2021 If there are multiple reocords for years then each array element for the year need to be an array of records while($stmt -> fetch()){ $awards[$ay][] = [$id, $an, $aw]; } or while($stmt -> fetch()){ $awards[$ay][$id] = [$an, $aw]; } 1 Quote Link to comment https://forums.phpfreaks.com/topic/314188-multidimentional-arrays-creating-and-looping-for-output/#findComment-1591875 Share on other sites More sharing options...
Adamhumbug Posted November 9, 2021 Author Share Posted November 9, 2021 Perfect answer as always. That did of course work, as for getting the data out of the other end, could you lend a hand with that also please? Quote Link to comment https://forums.phpfreaks.com/topic/314188-multidimentional-arrays-creating-and-looping-for-output/#findComment-1591876 Share on other sites More sharing options...
kicken Posted November 9, 2021 Share Posted November 9, 2021 Your top level array is a list of award years. Inside each year is a list of winners. If you want to loop through it all, just use two nested foreach loops. $awardYearsList = getExistingAwards(); foreach ($awardYearsList as $awardYear => $winnerList){ echo '<p>Winners in award year '.$awardYear.':</p>'; foreach ($winnerList as $winner){ echo '<p>'.$winner[0].' - '.$winner[1].'</p>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/314188-multidimentional-arrays-creating-and-looping-for-output/#findComment-1591877 Share on other sites More sharing options...
Adamhumbug Posted November 9, 2021 Author Share Posted November 9, 2021 Thank you for this. Using this method, is there anyway for me to reference the keys rather than the position. So for example when echoing out i could use $id, $ay, $aw? Quote Link to comment https://forums.phpfreaks.com/topic/314188-multidimentional-arrays-creating-and-looping-for-output/#findComment-1591878 Share on other sites More sharing options...
kicken Posted November 9, 2021 Share Posted November 9, 2021 If you want keys, you need to define them when you create the array. while($stmt -> fetch()){ $awards[$ay][$id] = ['an' => $an, 'aw' => $aw]; } Then you can use $winner['an'] / $winner['aw'] instead. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314188-multidimentional-arrays-creating-and-looping-for-output/#findComment-1591880 Share on other sites More sharing options...
Adamhumbug Posted November 9, 2021 Author Share Posted November 9, 2021 Thanks for this, didnt know that was a thing. Quote Link to comment https://forums.phpfreaks.com/topic/314188-multidimentional-arrays-creating-and-looping-for-output/#findComment-1591885 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.