FOREACH
With a simple syntax, it iterates through each key/value of arrays, making it one of the easiest ways to loop through arrays. FOREACH has two valid syntaxes;
Value
On each loop, the value of the current element of the array will be assigned to the $value variable.
FOREACH($array AS $value) {
// code to execute for each iteration
}
Key/Value
In addition to the first syntax, for each iteration, the key of the current element of the array will be assigned to the $key variable
FOREACH($array AS $key => $value) {
// code to execute for each iteration
}
Example;
$purchases = [
"02-02-2022" => "$299.99",
"02-22-2022" => "$59.99",
]
FOREACH ($purchases AS $date => $price) {
PRINT "Purchase of " . $price . " on " . $date
}
Copyright ©2013-2022 SunSed®. All rights reserved.