note 100661 deleted from ref.array by thiago
| From: | thiago@php.net | Date: | Thu, 28 Oct 2010 17:28:52 +0000 |
| Subject: | note 100661 deleted from ref.array by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173575@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
<html>
<head>
<title>Assignment 6</title>
</head>
<body>
<h3 style="color:red"> Exercise 1</h3>
<?php
//location and population
$row_color = array('red','green');
$pop = array('New York, NY' => 8008278,
'Los Angeles, CA' => 3694820,
'Chicago, IL' => 2896016,
'Houston, TX' => 1953631,
'Philadelphia, PA' => 1517550,
'Phoenix, AZ' => 1321045,
'San Diego, CA' => 1223400,
'Dallas, TX' => 1188580,
'San Antonio, TX' => 1144646,
'Detroit, MI' => 951270);
$totalpop = 0; // set to zero to start out with
print
"<table><tr><th>City</th><th>Population</th></tr>\n";
// table tags for organization
foreach ($pop as $city => $people) { // splitting the array. The foreach loop will continue
until it has gone through every item in the array
$totalpop += $people; // a little additioneroo
print
"<tr><td>$city</td><td>$people</td></tr>\n"; // table
tags again
}
print "<tr><td>Total</td><td>$totalpop</td></tr>\n";
// print out the total in a table
print "</table>\n";
?>
<br />
<br />
<h3 style="color:red"> Exercise 2.1</h3>
<?php
//much of the following is exactly the same as above ^^^
$pop = array('New York, NY' => 8008278,
'Los Angeles, CA' => 3694820,
'Chicago, IL' => 2896016,
'Houston, TX' => 1953631,
'Philadelphia, PA' => 1517550,
'Phoenix, AZ' => 1321045,
'San Diego, CA' => 1223400,
'Dallas, TX' => 1188580,
'San Antonio, TX' => 1144646,
'Detroit, MI' => 951270);
$totalpop = 0;
asort($pop); // The asort() function sorts an array by the values. In this case, the population
size
print
"<table><tr><th>City</th><th>Population</th></tr>\n";
foreach ($pop as $city => $people) {
$totalpop += $people;
print
"<tr><td>$city</td><td>$people</td></tr>\n";
}
print "<tr><td>Total</td><td>$totalpop</td></tr>\n";
print "</table>\n";
?>
<br />
<br />
<h3 style="color:red"> Exercise 2.2</h3>
<?php
$pop = array('New York, NY' => 8008278,
'Los Angeles, CA' => 3694820,
'Chicago, IL' => 2896016,
'Houston, TX' => 1953631,
'Philadelphia, PA' => 1517550,
'Phoenix, AZ' => 1321045,
'San Diego, CA' => 1223400,
'Dallas, TX' => 1188580,
'San Antonio, TX' => 1144646,
'Detroit, MI' => 951270);
$totalpop = 0;
ksort($pop); // The ksort() function sorts an array by the keys. In this case, the city name
print
"<table><tr><th>City</th><th>Population</th></tr>\n";
foreach ($pop as $city => $people) {
$totalpop += $people;
print
"<tr><td>$city</td><td>$people</td></tr>\n";
}
print "<tr><td>Total</td><td>$totalpop</td></tr>\n";
print "</table>\n";
?>
</body>
</html>