Jump to content

help with php array's


BrentonHale

Recommended Posts

I'm getting the following error messages with the code below:

 

Notice:  Undefined variable: state_totals in C:\wamp\www\chapter4_exercises_arrays.php on line 22Notice:  Undefined index: NY in C:\wamp\www\chapter4_exercises_arrays.php on line 22Notice:  Undefined index: CA in C:\wamp\www\chapter4_exercises_arrays.php on line 22Notice:  Undefined index: IL in C:\wamp\www\chapter4_exercises_arrays.php on line 22Notice:  Undefined index: TX in C:\wamp\www\chapter4_exercises_arrays.php on line 22Notice:  Undefined index: PA in C:\wamp\www\chapter4_exercises_arrays.php on line 22Notice:  Undefined index: AZ in C:\wamp\www\chapter4_exercises_arrays.php on line 22Notice:  Undefined index: MI in C:\wamp\www\chapter4_exercises_arrays.php on line 22

 

 

 

<?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 pre-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";

?>

Link to comment
Share on other sites

Try putting the following at the top:

$state_totals = array();

 

Thank you, it took off the error Notice:  Undefined variable: state_totals in C:\wamp\www\chapter4_exercises_arrays.php on line 23

 

However, i'm still getting this:

 

Notice:  Undefined index: NY in C:\wamp\www\chapter4_exercises_arrays.php on line 23

 

Notice:  Undefined index: CA in C:\wamp\www\chapter4_exercises_arrays.php on line 23

 

Notice:  Undefined index: IL in C:\wamp\www\chapter4_exercises_arrays.php on line 23

 

Notice:  Undefined index: TX in C:\wamp\www\chapter4_exercises_arrays.php on line 23

 

Notice:  Undefined index: PA in C:\wamp\www\chapter4_exercises_arrays.php on line 23

 

Notice:  Undefined index: AZ in C:\wamp\www\chapter4_exercises_arrays.php on line 23

 

Notice:  Undefined index: MI in C:\wamp\www\chapter4_exercises_arrays.php on line 23

 

Link to comment
Share on other sites

Branton:

The reason you are getting these errors is because you are trying to perform an + operation on a array key that does not exists yet. To get rid of the errors simply add the following line of code above line 26.

if(!isset($state_totals[$info['state']])) $state_totals[$info['state']]=0;

 

Dawsba:

To see the errors add the following lines of code at the top of the script

ini_set('display_errors', 1);
error_reporting(E_ALL);

 

Ajaxmonk

Clean, Custom PHP / Javascript

www.ajaxmonk.com

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.