BrentonHale Posted December 12, 2009 Share Posted December 12, 2009 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"; ?> Quote Link to comment Share on other sites More sharing options...
chaking Posted December 12, 2009 Share Posted December 12, 2009 Try putting the following at the top: $state_totals = array(); Quote Link to comment Share on other sites More sharing options...
BrentonHale Posted December 12, 2009 Author Share Posted December 12, 2009 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 Quote Link to comment Share on other sites More sharing options...
dawsba Posted December 12, 2009 Share Posted December 12, 2009 works for me, you could try long calling the array name $population[$info]['state'] Quote Link to comment Share on other sites More sharing options...
ajaxmonk Posted December 12, 2009 Share Posted December 12, 2009 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.