note 100661 added to ref.array
| From: | Anonymous at osu1 dot php dot net | Date: | Thu, 28 Oct 2010 17:22:58 +0000 |
| Subject: | note 100661 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-173572@lists.php.net to get a copy of this message | ||
<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>
----
Server IP: 64.71.164.2
Probable Submitter: 67.51.164.100
----
Manual Page -- http://www.php.net/manual/en/ref.array.php
Edit -- https://master.php.net/note/edit/100661
Del: integrated -- https://master.php.net/note/delete/100661/integrated
Del: useless -- https://master.php.net/note/delete/100661/useless
Del: bad code -- https://master.php.net/note/delete/100661/bad+code
Del: spam -- https://master.php.net/note/delete/100661/spam
Del: non-english -- https://master.php.net/note/delete/100661/non-english
Del: in docs -- https://master.php.net/note/delete/100661/in+docs
Del: other reasons-- https://master.php.net/note/delete/100661
Reject -- https://master.php.net/note/reject/100661
Search -- https://master.php.net/manage/user-notes.php