Jump to content

[SOLVED] Array from function only returns single value


rvdb86

Recommended Posts

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  :shrug:

 

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!

 

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>';
                }

 

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.