Skip navigation.
Home

PHP Arrays Ex. #7: Multi-Dimensional Array

This PHP exercise has two parts. In the first, you will take the large cities array from Arrays Ex. #5 and transform it into a multi-dimensional array. That will become the data in a table.

Rewrite the large cities array into a multi-dimensional array called $multiCity. The first sub-array will be completely new and include the labels, City, Country, Continent. (These will become the keys in the second half of this exercise.) Each of the succeeding sub-arrays should include those three items, one for each of the cities, for a total of 11 sub-arrays.

Here's the content for your array: City, Country, Continent; Tokyo, Japan, Asia; Mexico City, Mexico, North America; New York City, USA, North America; Mumbai, India, Asia; Seoul, Korea, Asia; Shanghai, China, Asia; Lagos, Nigeria, Africa; Buenos Aires, Argentina, South America; Cairo, Egypt, Africa; London, UK, Europe.

In the HTML, use the array in a table. The first row should be a header row and contain the entries in the first sub-array. Call these items without using a loop. For the succeeding rows, use a for loop with a foreach loop nested inside to populate the table with the remaining contents of the array. Use the count() function so that your for loop will function properly even if you increment or decrement the array.

Add a simple style sheet in the head section of your HTML. You can take on this challenge yourself or simply cut and paste the following between the <head></head> tags.

<style type="text/css">
td, th {width: 8em; border: 1px solid black; padding-left: 4px;}
th {text-align:center;}
table {border-collapse: collapse; border: 1px solid black;}
</style>

 

My solution.

Is there anything wrong with creating variables for all the arrays?

<?php
$multiCity = array(
	$a = array('City', 'Country', 'Continent'),
	$b = array('City'=>'Tokyo', 'Country'=>'Japan', 'Continent'=>'Asia'),
	$c = array('City'=>'Mexico City', 'Country'=>'Mexico', 'Continent'=>'North America'),
	$d = array('City'=>'New York City', 'Country'=>'USA', 'Continent'=>'North America'),
	$e = array('City'=>'Mumbai', 'Country'=>'India', 'Continent'=>'Asia'),
	$f = array('City'=>'Seoul', 'Country'=>'Korea', 'Continent'=>'Asia'),
	$g = array('City'=>'Shanghai', 'Country'=>'China', 'Continent'=>'Asia'),
	$h = array('City'=>'Lagos', 'Country'=>'Nigeria', 'Continent'=>'Africa'),
	$i = array('City'=>'Buenos Aires', 'Country'=>'Argentina', 'Continent'=>'South America'),
	$j = array('City'=>'Cairo', 'Country'=>'Egypt', 'Continent'=>'Africa'),
	$k = array('City'=>'London', 'Country'=>'UK', 'Continent'=>'Europe')
	);	
?>

<table class="multicity">
	<tr>
	<?php foreach($a as $th){echo "<th>$th</th>";}?>
	</tr>
	
	<?php for($x=1;$x<=count($multiCity);$x++){
		echo "<tr>";
		foreach($multiCity[$x] as $td){echo "<td>$td</td>";}
		}
		echo "</tr>";
		?>
	
	
</table>

there's nothing wrong with

there's nothing wrong with that but you did 75% of the work for PHP :)
make PHP does the job for you.

Question

foreach ( $multiCity[$row] as $value){
echo"$value";
}

for this foreach loop as show above, am i right to say that it will only shows the value and not the key of the array. Is that the standard output behaviour of the foreach code?

I probably should have read the instructions

as I did it differently, without using the for loop. However, this works and I think should be flexible (although I created the array using 'explode()' on the original string

<?php
$cities = "City, Country, Continent; Tokyo, Japan, Asia; Mexico City, Mexico, North America; New York City, USA, North America; Mumbai, India, Asia; Seoul, Korea, Asia; Shanghai, China, Asia; Lagos, Nigeria, Africa; Buenos Aires, Argentina, South America; Cairo, Egypt, Africa; London, UK, Europe";

$cities = explode(";", $cities);

$mdCities = array();

foreach($cities as $city){
$city = explode(", ", $city);
array_push($mdCities, $city);
}

?>
<table>
<thead>
<tr>
<?php foreach($mdCities[0] as $header){
echo "<th>" . $header . "</th>";
} ?>
</tr>
</thead>
<tbody>
<?php
array_shift($mdCities);
foreach($mdCities as $city){
echo "<tr>";
foreach($city as $detail){
echo "<td>" . $detail . "</td>";
}
} ?>
</tbody>
</table>

first try

Here's my solution for the data loop:

foreach($multiCity as $t){
if($t !== $multiCity[0]){
echo "";
echo "$t[0]";
echo "$t[1]";
echo "$t[2]";
echo "";
}
}

Not sure if better or worse (probably worse hehe), just thought I'd share.
I just recently started learning PHP and your site has already helped TREMENDOUSLY, as noone can get better without writing code.
Thanks for this and keep doing what you do, I'm looking forward to more complex exercises as I'm going trough these.

Minor Mistake

If you take a look at the output of your script I think you'll find that the city of London is missing from your table. In your script you are subtracting 1 from your count($multiCity) which is not necessary because you take care of counting out the header row by starting $row at 1 in your loop.

I am also wondering if there is any reason to write the foreach loop using $key=>$value. I wrote mine like this:

foreach ($multiCity[$row] as $m) {
echo "".$m."";
}

That seems to work just fine.

Thanks!

Thank you for catching that.

You are right, I accounted for the header row twice, thus leaving out London. It's changed now.

You're also right that in the first script, the loop does not access keys so doesn't need the $key variable in the loop. In the second script, the loop on line 60 does access keys, but again, the loop on line 75 doesn't.

Thanks again.