Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multidimensional Associative Arrays

Status
Not open for further replies.

tobyheywood

IS-IT--Management
Apr 20, 2001
122
GB
Hi,

I'm having a little difficulty with a multi-dimensional associative array. Basically I want to output a table which has a movie genre in bold in the first <td> and then the following <td>s will contain the movie titles themselves.

As you can tell this is a rather basic question but I would appreciate the help. I hav used the following code so far to almost acheive what I want except I cannot display the key/genre as a string it just shows as either Array or an integer.

Code:
<?php
$movies = array(
			&quot;SF&quot; => array(&quot;2001&quot;, &quot;Alien&quot;, &quot;Terminator&quot;),
			&quot;Comedy&quot; => array(&quot;Happy Gilmore&quot;, &quot;Meet the Parents&quot;, &quot;The Nutty Professor&quot;)
			);

print &quot;<table width=60% border=\&quot;0\&quot;>\n&quot;;

foreach ($movies as $val) {
	print &quot;<tr>&quot;;
	// what can I place here to print the relative key?
	foreach ($val as $key=>$movietitle) {
		print &quot;<td>$movietitle</td>\n&quot;;
	}
	print &quot;</tr>&quot;;
}
print &quot;</table>&quot;;
?>
Toby Heywood
 
This should do it:
[tt]foreach ($movies as $genre => $array) {
print &quot;<tr>&quot;;
print &quot;<td><b>$genre</b></td>&quot;;
foreach ($array as $movietitle) {
print &quot;<td>$movietitle</td>\n&quot;;
}
print &quot;</tr>&quot;;
}[/tt] //Daniel
 
Daniel,

You are the man! I spend hours staring at it, trying to work out what I could do. It never occured to me to reverse the foreach statements!

Thank you. Toby Heywood
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top