Jump to content

[SOLVED] Learning PHP 5 - author error, no errata


acialk

Recommended Posts

Hey guys,

 

I'm currently making my way through a book called 'Learning PHP 5'(2004) and the author's own answer to one of the exercises is outputting Undefined index. I can't see anything on the errata website for that book  :-\

 

It does output data below the error but what's causing the undefined error?

 

Here's the author's code:

 

<?php
// 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";
?>

 

Here's what I get when I run the code:

Notice: Undefined index: NY in C:\wamp\www\phpplay\answer-4-3.php on line 25

Notice: Undefined index: CA in C:\wamp\www\phpplay\answer-4-3.php on line 25

Notice: Undefined index: IL in C:\wamp\www\phpplay\answer-4-3.php on line 25

Notice: Undefined index: TX in C:\wamp\www\phpplay\answer-4-3.php on line 25

Notice: Undefined index: PA in C:\wamp\www\phpplay\answer-4-3.php on line 25

Notice: Undefined index: AZ in C:\wamp\www\phpplay\answer-4-3.php on line 25

Notice: Undefined index: MI in C:\wamp\www\phpplay\answer-4-3.php on line 25
City	Population
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
NY	8008278
CA	4918220
IL	2896016
TX	4286857
PA	1517550
AZ	1321045
MI	951270
Total	23899236

 

The problem is occurring because you're trying to add something to an index before it's there. Replace

<?php
$state_totals[$info['state']] += $info['pop'];
?>

with

<?php
if (array_key_exists($info['state'],$state_totals)) {
    $state_totals[$info['state']] += $info['pop'];
} else {
    $state_totals[$info['state']] = $info['pop'];
}
?>

which checks to see if the index is there first.

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.