Last Updated:

Foreach Loop in PHP

 

Among all the available loops in PHP, there is another narrow purpose that is called by the foreach statement. This loop executes a block of statements exclusively for arrays. The enumeration of the elements of the array is carried out in order - from zero to the last. However, you can skip actions on an element through the conditional statement and continue. In parentheses foreach, you do not need to specify the length of the array, but just specify its name. In any case, all iterations will pass through the entire length - no more, no less.

Foreach syntax

Foreach is syntactically similar to for, while, or dowhile, but the code structure in parentheses is different: foreach($arras $variable){operators} or foreach($arras $index=>$value).

Cycle structure:

  • foreach(){} is a loop declaration operator.
  • $arr is an array to be cycled.
  • as is the array-to-variable ratio operator.
  • $variable is a variable that stores both the index of an item at each iteration and its value.
  • $index=>$value (for associative arrays) — the first variable is the index of the associative array, the second is the value;
  • operators are the operators that will handle the array variable.

Example of array brute force:

<?php
$arr = array("London", "Moscow", "Paris", "New York"); Create an array with numeric indexes

foreach($arras $index){ // Variable $index stores the value of a single element of the echo iteration
"$index ";

}

Conclusion: London, Moscow, Paris, New York
?>

There were only 4 iterations in our loop according to the length of the array. With each iteration, the variable $index stores one current value of the array element.

Enumeration of associative arrays

 

As you know, the index of the array is not a serial number, but a string value: $arr["name"] => "Vasya". This array organization provides more semantic access to data based on logically related index names. To iterate through such an array in foreach, a slightly modified variable parameter construct is provided.

Example:

<?php
$weight_of_fruits = array(banana"=>"200g", "apple"=>"150g", "strawberry"=>"20g"); associative array of fruits and weights of each of them

foreach($weight_of_fruits as $fruit=>$weight){
echo «$fruit = $weigth «; display the name of the index and its value

}

Conclusion: banana = 200g apple = 150g strawberry = 20g
?>

Iterations may not be carried out for all elements, and depending on the condition, it is possible to limit the search to incomplete.

Example:

<?php
foreach($weight_of_fruitsas $fruit=>$weight){ // simple output of echo associative indexes
"$fruit"; \\ Output: banana apple strawberry

}

foreach($weight_of_fruits as $fruit=>$weight){
if($fruit == "strawberry") break; // searching to the penultimate echo element
"$fruit "; Conclusion: banana apple
}
?>

As you can see, we've limited the iteration of the array in the foreach loop to the last strawberry element. We can also skip the iteration over a specific element by setting the continue statement. Because it is not possible to access numeric indexes in this cycle, you will have to use a global variable to do so.

Example:

$arr = array(«London», «Moscow», «Paris», «New York»);
$x = 0; variable to store the
index foreach($arras $index){
if($x == 2) continue; // Skip iteration on the third
echo element "$arr[x]";
$x++; Increasing the value of the variable by 1
}
// Output will be implemented without the element Paris
// Output: London, Moscow, New York
?>

You cannot declare a variable $x inside a loop because it will revert to its initial value with each iteration. Therefore, it has to be announced in a global context. Of course, it is more convenient to use incomplete search in for loops. This will hide index variables locally in the block of the loop itself. However, incomplete brute forces in the foreach loop are created only with the numeric indexes of the array. Conditional iterations can be performed with an associative array.

Do not use foreach for multidimensional arrays, as you can easily lose the brute force logic. To do this, it is easier to use the nested for loop in for, so the algorithm will be easier to read and make edits.