php foreach loop array code example
Example 1: how to iterate through php array
$ar = ['Rudi', 'Morie', 'Halo', 'Miki'];
for ($i=0, $len=count($ar); $i<$len; $i++) {
echo "$ar[$i] \n";
}
Example 2: php loop array
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
unset($value);
?>
Example 3: php foreach array
$arr = array(1, 2, 3);
foreach ($arr as $key => $value) {
echo "{$key} => {$value} ";
}
Example 4: php foreach string in array
foreach($array as $item) {
echo $item['filename'];
echo $item['filepath'];
echo '<pre>'; var_dump($item);
}