Follow:  Follow PHPExercises on Twitter  Follow us on Facebook

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>

 

Comments

Here's my solution for the data loop:

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

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 no one 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 through these.

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>

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?

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

  1. <?php
  2. $multiCity = array(
  3. 	$a = array('City', 'Country', 'Continent'),
  4. 	$b = array('City'=>'Tokyo', 'Country'=>'Japan', 'Continent'=>'Asia'),
  5. 	$c = array('City'=>'Mexico City', 'Country'=>'Mexico', 'Continent'=>'North America'),
  6. 	$d = array('City'=>'New York City', 'Country'=>'USA', 'Continent'=>'North America'),
  7. 	$e = array('City'=>'Mumbai', 'Country'=>'India', 'Continent'=>'Asia'),
  8. 	$f = array('City'=>'Seoul', 'Country'=>'Korea', 'Continent'=>'Asia'),
  9. 	$g = array('City'=>'Shanghai', 'Country'=>'China', 'Continent'=>'Asia'),
  10. 	$h = array('City'=>'Lagos', 'Country'=>'Nigeria', 'Continent'=>'Africa'),
  11. 	$i = array('City'=>'Buenos Aires', 'Country'=>'Argentina', 'Continent'=>'South America'),
  12. 	$j = array('City'=>'Cairo', 'Country'=>'Egypt', 'Continent'=>'Africa'),
  13. 	$k = array('City'=>'London', 'Country'=>'UK', 'Continent'=>'Europe')
  14. 	);	
  15. ?>
  16.  
  17. <table class="multicity">
  18. 	<tr>
  19. 	<?php foreach($a as $th){echo "<th>$th</th>";}?>
  20. 	</tr>
  21.  
  22. 	<?php for($x=1;$x<=count($multiCity);$x++){
  23. 		echo "<tr>";
  24. 		foreach($multiCity[$x] as $td){echo "<td>$td</td>";}
  25. 		}
  26. 		echo "</tr>";
  27. 		?>
  28.  
  29.  
  30. </table>

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