Working with Indexed Arrays - Exercise

Contact Us or call 1-877-932-8228
Working with Indexed Arrays - Exercise

Working with Indexed Arrays

Duration: 10 to 15 minutes.

In this exercise, you will use arrays to create a table with a single column that lists all your favorite colors. As shown in the screenshot below, the background of each table row should be the same as the color named in the row.

Color Table

  1. Open Arrays/Exercises/ColorTable.php for editing.
  2. Create an array that holds your favorite colors.
  3. Inside of the open and close <table> tags, loop through the array outputting a table row for each element.
  4. Test your solution in a browser.

Solution:

Arrays/Solutions/ColorTable.php
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Color Table</title>
</head>

<body>
<h1>Color Table</h1>
<?php
	$favColors = array();
	$favColors[] = 'BlanchedAlmond';
	$favColors[] = 'CadetBlue';
	$favColors[] = 'BurlyWood';
	$favColors[] = 'DarkOliveGreen';
	$favColors[] = 'HotPink';
	$favColors[] = 'Papayawhip';
?>
<table border="1">
<?php
	foreach ($favColors as $color)
	{
		echo "<tr bgcolor='$color'><td>$color</td></tr>";
	}
?>
</table>
</body>
</html>
Next