Save phpfor.php as phpforeach.php and delete the first for loop (three lines).  Replace the second for loop with this:

foreach($george as $item){				
    echo "<p>$item</p>";

As you can see the code is briefer as the bit in brackets has changed.  The runs through the array in order as before but this time will stop automatically when it runs out. As well as being a condition the bit in brackets saves each value to a variable called $item each time through the loop.

In English you could say this as "Give me one item of data from the array at a time".


As mentioned arrays have values but they also have keys (indexes) which are used to pick which item of data you want.  You used numbers as keys but also words in associative arrays.  You can extract the keys as well as the values in the foreach loop by amending the bit in brackets like this (don't be confused by the => which is explained below):

foreach($george as $key=>$item){				
    echo "<p>Item number $key is $item</p>";

The => is not a comparison operator (like <=).  Think of => as being an arrow connecting the key and the value to make a pair.

Try that version in your page and then look at the code again.

In English this second version fo the loop could be "Give me one item of data and the matching index/key at a time".