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!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.