Write a PHP file that will display the contents of a two-dimensional array in an
ID: 3826990 • Letter: W
Question
Write a PHP file that will display the contents of a two-dimensional array in an HTML table. Your program should define an array that contains the state name, capital city, largest city, and population for five US states. You can find such a list here:
https://en.wikipedia.org/wiki/List_of_states_and_territories_of_the_United_States
The rows of the array should be indexed by the name of the state. All spaces in the name of a state or a city should be replaced by underscores - for example, New_York instead of New York.
Make sure that you upload your file into the public_html directory of your cislinux account.
Send me an email when you are finished that includes the full URL of your homework assignment.
Explanation / Answer
<html>
<body>
<?php
$state="state";
$capital="Capital";
$largest="Largest city";
$pop="Population";
$states = array ------ defining a 2D array
(
array("Alabama","Montgomery","Birmingham",4863300),
array( "Alaska","Juneau","Anchorage",741894),
array("Arizona","Phoenix","Phoenix",693107),
array("Arkansas","Little Rock","Little Rock",2988248),
array("California","Sacramento","Los Angeles",39250017)
);
echo '<table>';
echo '<tr>';
echo '<th>'.$state.'</th>';
echo '<th>'.$capital.'</th>';
echo '<th>'.$largest.'</th>';
echo '<th>'.$pop.'</th>';
echo '</tr>';
for($i=0;$i<count($states);$i++) { -------- displaying the array in a table
$states[$i][0] = str_replace(' ', '_', $states[$i][0]); -------- replacing blanck spaces with underscore
$states[$i][1] = str_replace(' ', '_', $states[$i][1]);
$states[$i][2] = str_replace(' ', '_', $states[$i][2]);
echo('<tr>');
echo('<td>' . $states[$i][0] . '</td>');
echo('<td>' . $states[$i][1] . '</td>');
echo('<td>' . $states[$i][2] . '</td>');
echo('<td>' . $states[$i][3] . '</td>');
echo('</tr>');
}
echo '</table>';
?>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.