while And each

each(), is similar to next() but returns false only after it has run out of array to traverse. each() returns an array, which holds keys and values for the key/value pair it is pointing to. The array that each() returns has four key/value pairs:

  • Key: 0
    Value: current-key
  • Key: 1
    Value: current-value
  • Key: ‘key’
    Value: current-key
  • Key: ‘value’
    Value: current-value

For the return value of each() the following example uses the keys ‘key’ and ‘value’. The example does not use the keys 0 and 1.

function print_key_and_value($city_array) {
/*
reliably prints everything in array
*/
  reset($city_array);
  while ($array_cell = each($city_array)) {
    $current_value = $array_cell['value'];
    $current_key = $array_cell['key'];
    print("Key: $current_key; Value: $current_value<br />");
  }
}

print_keys_and_values_each($major_city_info);

 
Output:

Key: 0; Value: Caracas
Key: Caracas; Value: Venezuela
Key: 1; Value: Paris
Key: Paris; Value: France
Key: 2; Value: Tokyo
Key: Tokyo; Value: Japan
Advertisements

About samehramzylabib

See About on https://samehramzylabib.wordpress.com
This entry was posted in Coding, PHP Arrays and tagged , , , . Bookmark the permalink.

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s