Trouble with multidimensional arrays in PHP

Trouble with multidimensional arrays in PHP

Post by griff.mill » Fri, 18 Jun 2004 05:40:19


I have always had trouble with MD arrays. If I do this:

<?PHP

$i=0;
$i_sitename=$i++;
$i_address1=$i++;
$i_address2=$i++;
$i_city=$i++;
$i_state=$i++;
$i_zip=$i++;
$i_country=$i++;
$i_telephone=$i++;

$i=0;
$siteinfo[$i][$i_sitename]="Crusty Pizza Parlor";
$siteinfo[$i][$i_address1]="123 9th Street";
$siteinfo[$i][$i_address2]="Suite 325";
$siteinfo[$i][$i_city]="Springfield";
$siteinfo[$i][$i_state]="HI";
$siteinfo[$i][$i_zip]="12345";
$siteinfo[$i][$i_country]="USA";
$siteinfo[$i][$i_telephone]="123-456-7890";

$i++;
$siteinfo[$i][$i_sitename]="Acme Anvils";
$siteinfo[$i][$i_address1]="601 Warner";
$siteinfo[$i][$i_address2]="";
$siteinfo[$i][$i_city]="La Jolla";
$siteinfo[$i][$i_state]="CA";
$siteinfo[$i][$i_zip]="99999";
$siteinfo[$i][$i_country]="USA";
$siteinfo[$i][$i_telephone]="651-555-1212";

$i++;
$siteinfo[$i][$i_sitename]="ABC Musical Instrument and Gun Repair";
$siteinfo[$i][$i_address1]="555 12th Avenue";
$siteinfo[$i][$i_address2]="";
$siteinfo[$i][$i_city]="Walla Walla";
$siteinfo[$i][$i_state]="WA";
$siteinfo[$i][$i_zip]="30038";
$siteinfo[$i][$i_country]="USA";
$siteinfo[$i][$i_telephone]="440-668-3745";

echo "<HTML>\n";
echo "<HEAD>\n";
echo " <TITLE>foo";
echo "</TITLE>\n";
echo "</HEAD>\n";
echo "<BODY TEXT=\"BLACK\" BGCOLOR=\"SILVER\" LINK=\"BLUE\"
VLINK=\"NAVY\" ALINK=\"RED\">\n<UL>\n";

/* Build a menu of sites. */
for ($i = 0; $i < count($siteinfo); $i++) {
echo " <LI>";
echo "$siteinfo[$i][$i_sitename]\n";
#$workarray=$siteinfo[$i];
#echo "$workarray[$i_sitename]\n";
}
echo "</UL>\n";
echo "<P>\n";

echo "</BODY>\n";
echo "</HTML>\n";

?>

I get this:

<HTML>
<HEAD>
<TITLE>foo</TITLE>
</HEAD>
<BODY TEXT="BLACK" BGCOLOR="SILVER" LINK="BLUE" VLINK="NAVY"
ALINK="RED">
<UL>
<LI>Array[0]
<LI>Array[0]
<LI>Array[0]
</UL>
<P>
</BODY>
</HTML>

If I access the siteinfo array indirectly, I can make it work (see the
commented-out lines in the loop) by first setting $workarray and then
accessing it instead.

Why can't I directly access e.g. $siteinfo[0][0] and have it work?

Griff
 
 
 

Trouble with multidimensional arrays in PHP

Post by griff.mill » Sat, 19 Jun 2004 00:03:08

I figured it out. Instead of:

echo "$siteinfo[$i][$i_sitename]\n";

...I need to do:

echo $siteinfo[$i][$i_sitename];
echo "\n";



Griff

 
 
 

Trouble with multidimensional arrays in PHP

Post by griff.mill » Thu, 24 Jun 2004 04:18:25


BTW, can anyone tell me why I have to do it the latter way?

Griff