Created
January 29, 2025 02:48
-
-
Save Mwamitovi/8a108808409b900d97583022e443fe7b to your computer and use it in GitHub Desktop.
learningPHP: Chapter-4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Chapter-4: Exercises | |
// Question-1 | |
// 1. According to the US Census Bureau, the 10 largest American cities (by population) in 2010 were as follows: | |
// • New York, NY (8,175,133 people) | |
// • Los Angeles, CA (3,792,621) | |
// • Chicago, IL (2,695,598) | |
// • Houston, TX (2,100,263) | |
// • Philadelphia, PA (1,526,006) | |
// • Phoenix, AZ (1,445,632) | |
// • San Antonio, TX (1,327,407) | |
// • San Diego, CA (1,307,402) | |
// • Dallas, TX (1,197,816) | |
// • San Jose, CA (945,942) | |
// | |
// Define an array (or arrays) that holds this information about locations and populations. | |
// Print a table of locations and population information that includes the total population in all 10 cities. | |
// Answer-1 ?> | |
<table> | |
<tr> | |
<th>City</th> | |
<th>Population</th> | |
</tr> | |
<?php | |
$census = [ | |
'New York, NY' => 8175133, | |
'Los Angeles, CA' => 3792621, | |
'Chicago, IL' => 2695598, | |
'Houston, TX' => 2100263, | |
'Philadelphia, PA' => 1526006, | |
'Phoenix, AZ' => 1445632, | |
'San Antonio, TX' => 1327407, | |
'San Diego, CA' => 1307402, | |
'Dallas, TX' => 1197816, | |
'San Jose, CA' => 945942 | |
]; | |
$total = 0; | |
foreach ($census as $city => $population) { | |
$total += $population; | |
print "<tr><td>$city</td><td>$population</td></tr>\n"; | |
} | |
print "<tr><td>Total</td><td>$total</td></tr>\n"; | |
print "</table>"; | |
?> | |
<?php | |
// Question-2 | |
// 2. Modify your solution to the previous exercise so that the rows in the result table are ordered by population. | |
// Then modify your solution so that the rows are ordered by city name. | |
// Answer-2 | |
$census = [ | |
'New York, NY' => 8175133, | |
'Los Angeles, CA' => 3792621, | |
'Chicago, IL' => 2695598, | |
'Houston, TX' => 2100263, | |
'Philadelphia, PA' => 1526006, | |
'Phoenix, AZ' => 1445632, | |
'San Antonio, TX' => 1327407, | |
'San Diego, CA' => 1307402, | |
'Dallas, TX' => 1197816, | |
'San Jose, CA' => 945942 | |
]; | |
// Sort the associative array by value | |
asort($census); | |
print "<table>\n"; | |
print "<tr><th>City</th><th>Population</th></tr>\n"; | |
$total = 0; | |
foreach ($census as $city => $population) { | |
$total += $population; | |
print "<tr><td>$city</td><td>$population</td></tr>\n"; | |
} | |
print "<tr><td>Total</td><td>$total</td></tr>\n"; | |
print "</table>"; | |
// Sort the associative array by key | |
ksort($census); | |
print "<table>\n"; | |
print "<tr><th>City</th><th>Population</th></tr>\n"; | |
$total = 0; | |
foreach ($census as $city => $population) { | |
$total += $population; | |
print "<tr><td>$city</td><td>$population</td></tr>\n"; | |
} | |
print "<tr><td>Total</td><td>$total</td></tr>\n"; | |
print "</table>"; | |
// Question-3 | |
// 3. Modify your solution to the first exercise so that the table also contains rows that hold state population totals for each state represented in the list of cities. | |
// Answer-3 ?> | |
<table> | |
<tr> | |
<th>City</th> | |
<th>Population</th> | |
</tr><?php | |
// Each element in $census is a three-element array containing city name, state, and population | |
$census = [ | |
['New York', 'NY', 8175133], | |
['Los Angeles', 'CA', 3792621], | |
['Chicago', 'IL', 2695598], | |
['Houston', 'TX', 2100263], | |
['Philadelphia', 'PA', 1526006], | |
['Phoenix', 'AZ', 1445632], | |
['San Antonio', 'TX', 1327407], | |
['San Diego', 'CA', 1307402], | |
['Dallas', 'TX', 1197816], | |
['San Jose', 'CA', 945942] | |
]; | |
$total = 0; | |
$state_totals = array(); | |
foreach ($census as $city_info) { | |
// Update the total population | |
$total += $city_info[2]; | |
// If we haven't seen this state yet, initialize its population total to 0 | |
if (!array_key_exists($city_info[1], $state_totals)) { | |
$state_totals[$city_info[1]] = 0; | |
} | |
// Update the per-state population | |
$state_totals[$city_info[1]] += $city_info[2]; | |
print "<tr><td>$city_info[0], $city_info[1]</td><td>$city_info[2]</td></tr>\n"; | |
} | |
print "<tr><td>Total</td><td>$total</td></tr>\n"; | |
// Print the per-state totals | |
foreach ($state_totals as $state => $population) { | |
print "<tr><td>$state</td><td>$population</td></tr>\n"; | |
} | |
print "</table>"; | |
// Question-4 | |
// 4. For each of the following kinds of information, | |
// state how you would store it in an array and then give sample code that creates such an array with a few elements. | |
// For example, for the first item, you might say, “An associative array whose key is the student’s name | |
// and whose value is an associative array of grade and ID number,” as in the following: | |
$students = [ | |
'James D. McCawley' => ['grade' => 'A+', 'id' => 271231], | |
'Buwei Yang Chao' => ['grade' => 'A', 'id' => 818211] | |
]; | |
// Answer-4 | |
// a. The grades and ID numbers of students in a class | |
/* The grades and ID numbers of students in a class: | |
An associative array whose key is the student's name and whose value is an associative array of grade and ID number | |
*/ | |
$students = [ | |
'James D. McCawley' => [ 'grade' => 'A+','id' => 271231 ], | |
'Buwei Yang Chao' => [ 'grade' => 'A', 'id' => 818211] | |
]; | |
// b. How many of each item in a store inventory are in stock | |
/* How many of each item in a store inventory are in stock: | |
An associative array whose key is the item name and whose value is the number in stock | |
*/ | |
$inventory = [ 'Wok' => 5, 'Steamer' => 3, 'Heavy Cleaver' => 3, 'Light Cleaver' => 0 ]; | |
// c. School lunches for a week: the different parts of each meal (entrée, side dish, drink, etc.) and the cost for each day | |
/* School lunches for a week — the different parts of each meal (entree, side dish, drink, etc.) and the cost for each day: | |
An associative array whose key is the day and whose value is an associative array describing the meal. | |
This associative array has a key/value pair for cost and a key/value pair for each part of the meal. | |
*/ | |
$lunches = [ | |
'Monday' => [ | |
'cost' => 1.50, | |
'entree' => 'Beef Shu-Mai', | |
'side' => 'Salty Fried Cake', | |
'drink' => 'Black Tea' | |
], | |
'Tuesday' => [ | |
'cost' => 2.50, | |
'entree' => 'Clear-steamed Fish', | |
'side' => 'Turnip Cake', | |
'drink' => 'Bubble Tea' | |
], | |
'Wednesday' => [ | |
'cost' => 2.00, | |
'entree' => 'Braised Sea Cucumber', | |
'side' => 'Turnip Cake', | |
'drink' => 'Green Tea' | |
], | |
'Thursday' => [ | |
'cost' => 1.35, | |
'entree' => 'Stir-fried Two Winters', | |
'side' => 'Egg Puff', | |
'drink' => 'Black Tea' | |
], | |
'Friday' => [ | |
'cost' => 3.25, | |
'entree' => 'Stewed Pork with Taro', | |
'side' => 'Duck Feet', | |
'drink' => 'Jasmine Tea' | |
] | |
]; | |
// d. The names of people in your family | |
/* The names of people in your family: | |
A numeric array whose indices are implicit and whose values are the names of family members | |
*/ | |
$family = [ 'Bart', 'Lisa', 'Homer', 'Marge', 'Maggie' ]; | |
// e. The names, ages, and relationship to you of people in your family | |
/* The names, ages, and relationship to you of people in your family: | |
An associative array whose keys are the names of family members and whose | |
values are associative arrays with age and relationship key/value pairs | |
*/ | |
$family = [ | |
'Bart' => [ | |
'age' => 10, | |
'relation' => 'brother' | |
], | |
'Lisa' => [ | |
'age' => 7, | |
'relation' => 'sister' | |
], | |
'Homer' => [ | |
'age' => 36, | |
'relation' => 'father' | |
], | |
'Marge' => [ | |
'age' => 34, | |
'relation' => 'mother' | |
], | |
'Maggie' => [ | |
'age' => 1, | |
'relation' => 'self' | |
] | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment