For each small question

Hello

I have an small doubt on how for each works :lol:

Below is a 2D array.


$data[0][1]="lion";
$data[0][2]="kenya";

extracting the above array

foreach( $data as $key => $value){

now what does this โ€˜$data as $key => $valueโ€™ mean in plain english :stuck_out_tongue:

may be an stupid question :wink:

It is very well explained there. :slight_smile:
Google is your friend.

Thats actually a multidimensional array. There is one object in $data. But within $data[0], there is another array.


foreach($data as $row) {
//this a basic for each, it will allow you to access only the data within the array
}

foreach($data as $key => $value) {
//this operates the same way, except now I can reference the name of the object you are on, where $key is the name of the object and $value is the data within
}

The following script is one way that PHP simplies writing a HTML menu:



   $aMenu = array
    (
      'home'   => 'Home page ',
      'info'     => 'Information';
      'contact' => 'Contact Us',
      'blog'     => 'Blog items',
   // 'debug'  => 'Debug not availalable online', // remove comment to add to menu
    );
  echo '<ul class="menu_top">';
     foreach($aMenu as $link => $item):
        echo '<a href="' . $link .'.html">' .$item .'</a>';
     endforeach;
  echo '</ul>';


Adding another link just requires copying, pasting and editing an array element.