rvdb86 Posted October 13, 2009 Share Posted October 13, 2009 Hi hope some one with a bit more experience with arrays can help me out with this: I Have a multidimensional array that is populated from a database. The problem is that only the first result of database is returned from the function. When I used the code outside the function it printed the information correctly Here is the code: function listAllApartments() { $allApartments=array(); while($row = mysql_fetch_array($result)) { // Create variables $apId = $row['Id']; $apAddress = $row['Address']; // Get all information from types table $Types = explode(', ', $row['typeList']); foreach($Types AS $Type) { // Split the type name and type price $TypeSplit = explode ('~',$Type); $apType = $TypeSplit[0]; $apPrice = $TypeSplit[1]; } // Get all appliances for apartment $Appliances = explode(', ', $row['appliancesList']); $i = 0; foreach($Appliances AS $Appliance) { // Create and populate array allAparments > apId > Appliances $allApartments[$apId]["Appliances"][$i] = $Appliance; $i++; } $allApartments[$apId]["Address"] = $apAddress; $allApartments[$apId]["Type"] = $apType; $allApartments[$apId]["Price"] = $apPrice; return $allApartments; /* foreach( $allApartments as $key => $v ) { echo $v["Address"]."<br />\n"; echo $v["Type"]."<br />\n"; echo $v["Price"]."<br />\n"; foreach( $allApartments[$apId]["Appliances"] as $a ) { echo $a."<br />\n"; } echo "<br />\n"; } */ } The commented out code at the end is the code i used outside the function and it worked correctly. Any suggestions are very much appreciated! TIA! Link to comment https://forums.phpfreaks.com/topic/177520-solved-array-from-function-only-returns-single-value/ Share on other sites More sharing options...
rvdb86 Posted October 13, 2009 Author Share Posted October 13, 2009 Sorry I forgot to add the code that calls the function and prints the array: $allApartments = listAllApartments(); foreach( $allApartments as $v ) { echo '<li><h3>'.$v["Address"].' - '.$v["Type"].'</h3></li>'; } Link to comment https://forums.phpfreaks.com/topic/177520-solved-array-from-function-only-returns-single-value/#findComment-935984 Share on other sites More sharing options...
trq Posted October 13, 2009 Share Posted October 13, 2009 Second time Ive answered this same question today (different poster though). return exits te function when it is called returning its argument. Move it outside of your while loop if you would like all the results returned. Link to comment https://forums.phpfreaks.com/topic/177520-solved-array-from-function-only-returns-single-value/#findComment-935988 Share on other sites More sharing options...
rvdb86 Posted October 13, 2009 Author Share Posted October 13, 2009 Sorry for double posting! Problem solved... I think I have just been looking at code for to long today, I should have realized the problem myself! Thanks! Link to comment https://forums.phpfreaks.com/topic/177520-solved-array-from-function-only-returns-single-value/#findComment-935992 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.