how to use foreach php code example
Example 1: php foreach
$arr = ['Item 1', 'Item 2', 'Item 3'];
foreach ($arr as $item) {
var_dump($item);
}
Example 2: foreach loop array php
foreach (array as $value){
print("value : $value");
}
foreach (array as $key => $value){
print("key[$key] => $value");
}
Example 3: foreach php
foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement
Example 4: for each php
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
foreach ($arr as $key => $value) {
echo "{$key} => {$value} ";
print_r($arr);
}
?>
Example 5: for each php
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
unset($value);
?>
Example 6: foreach in php
<?php
$arr = array("green", "blue", "pink", "white");
foreach ($arr as $element) {
echo "$element ";
}
?>