php foreach
$arr = ['Item 1', 'Item 2', 'Item 3'];
foreach ($arr as $item) {
var_dump($item);
}
foreach php
foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement
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");
}
pro každou smyčku php
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
pro každý php
<?php
$array = [
[1, 2],
[3, 4],
];
foreach ($array as list($a, $b)) {
// $a contains the first element of the nested array,
// and $b contains the second element.
echo "A: $a; B: $b\n";
}
?>
php foreac
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
?>