The information presented in arrays can differ in the type of values โโand their size, and the number of elements cannot always be determined in advance. Modern programming, especially in a distributed version, allows you to create complex data structures, the contents and properties of which can be determined dynamically at an indefinite point in time as a result of various actions or events, in a different sequence.
It is not always possible at the development stage to predict the operation process, to provide for all possible options for the presentation and use of information, the dynamics of their appearance and use.
Content loop syntax
Formulating the syntax foreach, PHP proposed two options for accessing elements. Both are independent of either the key type or the type of value and can be emulated in a regular loop. It is proposed to consider the array as a set of elements, the number of which is not initially determined. An array can be formed on the fly, with or without keys. An element can be deleted in the array, keys can be associative and formed by default.
foreach ($ aArrayName as $ xValue) {loop body}
This design requires the foreach PHP loop to go through all the elements in a row. In the body of the loop, the variable $ xValue will sequentially take all the values โโof the $ aArrayName array in the order in which they were added. Element key values โโwill not be available.
foreach ($ aArrayName as $ xKey => $ xValue) {loop body}
Here, also, executing the foreach construct, PHP will look through the entire contents of the array, but in the body of the loop, both the $ xValue variable and the $ xKey variable will be the element key in pairs.
Sequence of elements
Inside foreach, PHP will offer the content in the order in which the elements were added, but if during the formation of the array there were repeated additions / deletions, while something was added with the keys and something without, then it is best to work with the array not positions of a sequence of elements, but based on their content or on keys.
For various objective reasons, the sequence within the array may not be observed and / or may not have special significance, but it should not be oriented in any case. In simple tasks, on trivial data sets, there are no problems, and the algorithm can be configured for sequential processing, but when many factors influence the process of creating / editing an array, you should focus on the content.
Modern โrightโ elements
From the standpoint of the established own concept, without taking into account even unconditionally similar languages, the PHP foreach array must be independently designed taking into account the real concrete task.
Practice, when there is a given, but the given has an index in the general collection of similar ones by a certain criterion, it was yesterday .
The index became the key, and the array took the form of an associative array. That is, the key lost its sequential uniqueness (usually it was sequential: 0, 1, 2, ... n) and became also a value, but a simple value (that is, a key) associated with the real value (that is, the content of the element). It is today, it is right, but not perfect .
That is why the foreach loop is considered by PHP as an alternative to the regular loop oriented to regular arrays. This is first of all, and this is very important, because the real validity of the elements of the array , as well as their keys, follows from this!
Correct Arrays of Regular Elements
First there was an element, then two elements ... this is how an array of elements appeared and a cycle through an array of those:
for ($ i = 0; $ i <count ($ aArrayName); $ i ++) {
processing body of each $ aArrayName [$ i]
}
Then, instead of the faceless 0, 1, 2, ... n, the element got its own name - the key, and then the arrays became associative and then the foreach loop was needed - โeach loopโ:
foreach ($ aArrayName as $ xKey => $ xValue) {
processing body of each $ aArrayName [$ xKey] or $ xValue which is the same
}
Now the time has come when the correct elements should come to the array , that is, those that are on their own. They themselves know their index, their content, their place in the sequence, tend to show their own choice of sequence and delegate all these capabilities to the array itself that contains them.
Such regular arrays will be processed on their own. There will simply be no particular need to use regular loops and loops for each. Formally, the syntax and semantics already allow this, the question is only the inertia of the developerโs consciousness.