I hope someone can help me determine how to initialize this variable ($ value) from this example script. I copied a beginner book from Vikram Vaswani from PHP.
I have included the display_erors value in php.ini and it returns this error in the browser.
Note: Undefined variable: cities in C: \ BitNami \ wordpress-3.6-0 \ apache2 \ htdocs \ associative-array.php on line 23
Warning: invalid argument provided by foreach () in C: \ BitNami \ wordpress-3.6-0 \ apache2 \ htdocs \ associative-array.php on line 23
Here is my code copied from page 94 of this book
<?php // define array $citites = array( "United Kingdom" => "London", "United States" => "Washington DC", "France" => "Paris", "India" => "Delhi" ); // Iterate over the associative array // and print each value // this example as supplied in the book // returns uninitialized error for either $key or $value on line 20 foreach ($cities as $key => $value) { echo "$value is in $key. \r\n"; } ?>
Also in the same chapter, this other example, as copied, for the "array iterator" simply hangs endlessly in the browser. UP until all the examples from the book seemed to work perfectly.
This is the code copied from a book for an example of an array iterator. Does anyone know why this hangs endlessly, and the performers do not output output to the browser. Many thanks for the help.
<?php // define associative array (hash) $cities = array( "United States" => "Washington", "United Kingdom" => "London", "France" => "Paris", "Spain" => "Madrid", "Italy" => "Rome" ); // Create an array itterator object $iterator = new ArrayIterator($cities); // rewind to beginning of array $iterator->rewind(); // iterate over the array // print each value while ($iterator->valid()) { print $iterator->current() . " is in " . $iterator->key() . ". \r\n"; $iterator->next; } ?>
source share