loop through list php code example

Example 1: php loop through array

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

Example 2: foreach loop array php

foreach (array as $value){ 
  //code to be executed; 
  print("value : $value");
} 

foreach (array as  $key => $value){ 
  //code to be executed; 
  print("key[$key] => $value");
}

Example 3: how to iterate through php array

$ar = ['Rudi', 'Morie', 'Halo', 'Miki'];

for ($i=0, $len=count($ar); $i<$len; $i++) {
    echo "$ar[$i] \n";
}
/*
Rudi 
Morie 
Halo 
Miki 
*/

Example 4: loop through values of hash php

<?php
    while (list($var, $val) = each($array)) {
        print "$var is $val\n";
    }
?>

Tags:

Php Example