DekGenius.com
Previous Section  < Day Day Up >  Next Section

C.3 Chapter 4

C.3.1 Exercise 1:

$population = 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);
$total_population = 0;
print "<table><tr><th>City</th><th>Population</th></tr>\n";
foreach ($population as $city => $people) {
    $total_population += $people;
    print "<tr><td>$city</td><td>$people</td></tr>\n";
    
}
print "<tr><td>Total</td><td>$total_population</td></tr>\n";
print "</table>\n";

C.3.2 Exercise 2:

  1. Use asort( ) to sort by population.

    $population = 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);
    $total_population = 0;
    asort($population);
    print "<table><tr><th>City</th><th>Population</th></tr>\n";
    foreach ($population as $city => $people) {
        $total_population += $people;
        print "<tr><td>$city</td><td>$people</td></tr>\n";
        
    }
    print "<tr><td>Total</td><td>$total_population</td></tr>\n";
    print "</table>\n";

  2. Use ksort( ) to sort by city name.

    $population = 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);
    $total_population = 0;
    ksort($population);
    print "<table><tr><th>City</th><th>Population</th></tr>\n";
    foreach ($population as $city => $people) {
        $total_population += $people;
        print "<tr><td>$city</td><td>$people</td></tr>\n";
        
    }
    print "<tr><td>Total</td><td>$total_population</td></tr>\n";
    print "</table>\n";

C.3.3 Exercise 3:

// Separate the city and state name in the array so we can total by state
$population = array('New York'     => array('state' => 'NY', 'pop' => 8008278),
                    'Los Angeles'  => array('state' => 'CA', 'pop' => 3694820),
                    'Chicago'      => array('state' => 'IL', 'pop' => 2896016),
                    'Houston'      => array('state' => 'TX', 'pop' => 1953631),
                    'Philadelphia' => array('state' => 'PA', 'pop' => 1517550),
                    'Phoenix'      => array('state' => 'AZ', 'pop' => 1321045),
                    'San Diego'    => array('state' => 'CA', 'pop' => 1223400),
                    'Dallas'       => array('state' => 'TX', 'pop' => 1188580),
                    'San Antonio'  => array('state' => 'TX', 'pop' => 1144646),
                    'Detroit'      => array('state' => 'MI', 'pop' => 951270));
// Use the $state_totals array to keep track of per-state totals
$state_totals = array( );
$total_population = 0;
print "<table><tr><th>City</th><th>Population</th></tr>\n";
foreach ($population as $city => $info) {
    // $info is an array with two elements: pop (city population)
    // and state (state name)
    $total_population += $info['pop'];
    // increment the $info['state'] element in $state_totals by $info['pop']
    // to keep track of the total population of state $info['state']
    $state_totals[$info['state']] += $info['pop'];
    print "<tr><td>$city, {$info['state']}</td><td>{$info['pop']}</td></tr>\n";
    
}
// Iterate through the $state_totals array to print the per-state totals
foreach ($state_totals as $state => $pop) {
    print "<tr><td>$state</td><td>$pop</td>\n";
}
print "<tr><td>Total</td><td>$total_population</td></tr>\n";
print "</table>\n";

C.3.4 Exercise 4:

  1. An associative array whose keys are students' names and whose values are associative arrays of grade and ID number.

    $students = array('James D. McCawley' => array('grade' => 'A+',
                                                   'id' => 271231),
                      'Buwei Yang Chao' => array('grade' => 'A', 
                                                 'id' => 818211));

  2. An associative array whose key is the item name and whose value is the number in stock.

    $stock = array('Woks' => 5, 'Steamers' => 3, 'Heavy Cleavers' => 2, 
                   'Light Cleavers' => 6);

  3. 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 (entree, side dish, drink).

    $lunches = array('Monday' => array('cost' => 1.50,
                                       'entree' => 'Beef Shiu-Mai',
                                       'side' => 'Salty Fried Cake',
                                       'drink' => 'Black Tea'),
                     'Tuesday' => array('cost' => 1.50,
                                        'entree' => 'Clear-steamed Fish',
                                        'side' => 'Turnip Cake',
                                        'drink' => 'Black Tea'),
                     'Wednesday' => array('cost' => 2.00,
                                          'entree' => 'Braised Sea Cucumber',
                                          'side' => 'Turnip Cake',
                                          'drink' => 'Green Tea'),
                     'Thursday' => array('cost' => 1.35,
                                         'entree' => 'Stir-fried Two Winters',
                                         'side' => 'Egg Puff',
                                         'drink' => 'Black Tea'),
                     'Friday' => array('cost' => 2.15,
                                       'entree' => 'Stewed Pork with Taro',
                                       'side' => 'Duck Feet',
                                       'drink' => 'Jasmine Tea'));

  4. A numeric array whose values are the names of family members.

    $family = array('Bart','Lisa','Homer','Marge','Maggie');

  5. An associative array whose keys are the names of family members and whose values are associative arrays of age and relationship.

    $family = array('Bart' => array('relation' => 'brother',
                                    'age' => 10),
                    'Lisa' => array('relation' => 'sister',
                                    'age' => 7),
                    'Homer' => array('relation' => 'father',
                                     'age' => 36),
                    'Marge' => array('relation' => 'mother',
                                     'age' => 34),
                    'Maggie' => array('relation' => 'self',
                                      'age' => 1));

    Previous Section  < Day Day Up >  Next Section