Use foreach to loop through arrays.

Syntax:

foreach ($array as $value)
{
  code to be executed;
}

Example:

<?php
$x=array('one','two','three');
foreach ($x as $thisloop)
{
  print $thisloop . '<br />';
}
?>

About the example:

  • Begin with foreach, which is a PHP keyword
  • Followed by parentheses (),  inside which:
    $x (the array to loop)
    as (PHP keyword)
    $thisloop (variable containing value of current element in $x)
    — The name $thisloop is arbitrary, you can give it any variable name
  • Followed  by a code block, like this: {}
    — Code block contains code which executes once for each element in the looped array
    — Note, code blocks are not followed by semicolon