foreach php key code example

Example 1: php loop through array

$clothes = array("hat","shoe","shirt");
foreach ($clothes as $item) {
	echo $item;
}

Example 2: php foreach echo key value

foreach($page as $key => $value) {
  echo "$key is at $value";
}

Example 3: loop through values of hash php

foreach ($array as $key => $val) {
    print "$key = $val\n";
}

Example 4: php array loop

for ($i = 0; $i < count($array); $i++) {
    echo $array[$i]['filename'];
    echo $array[$i]['filepath'];
}

Example 5: php array loop

foreach($array as $item) {
    echo $item['filename'];
    echo $item['filepath'];

    // to know what's in $item
    echo '<pre>'; var_dump($item);
}