The information presented in the arrays maydiffer in the type of values and their size, and the number of elements is not always possible to determine in advance. Modern programming, especially in the distributed version, allows you to create complex data structures, the content and properties of which can be determined dynamically at an indefinite point in time as a result of various actions or events in different sequences.
It is not always possible at the development stage to predict the operation process, to foresee all possible options for the presentation and use of information, the dynamics of their appearance and use.
In phrasing foreach syntax, PHP proposed twooptions for accessing items. Both are independent of the type of the key or the type of the value and can be emulated by a normal loop. It is proposed to consider an array as a set of elements, the number of which is not initially defined. An array can be formed on the fly, with or without keys. An element can be deleted in the array, the keys can be associative and formed by default.
foreach ($ aArrayName as $ xValue) {loop body}
Such a construction requires the PHP foreach loop.go through all the elements in a row. In the body of the loop, the $ xValue variable will successively take all the values of the $ aArrayName array in the order in which they were added. Values of item keys will not be available.
foreach ($ aArrayName as $ xKey => $ xValue) {loop body}
Here also, executing the foreach construct, phpwill scan the entire contents of the array, but in the body of the loop, the corresponding values of both the $ xValue variable and the $ xKey variable, the key of the element, will take pairs.
Inside foreach PHP will offer content inthe order in which the elements were added, but if during the formation of the array there were repeated additions / deletions, and something was added with the keys, and something without, then it is best to work with the array not from the standpoint of a sequence of elements, but based on their contents or on the keys.
Due to various objective reasonsthe sequence inside the array may not be respected and / or may not have much importance, but it should not be guided in any case. In simple tasks, on trivial data sets, there are no problems, and the algorithm can be set up for sequential processing, but when many factors influence the process of creating / editing an array, you should focus on the content.
From the standpoint of the established own concept, without even unconditional-like languages being taken into account, the PHP foreach array needs to be designed independently, taking into account the real concrete task.
Practice, when there is this, and this one has an index in the general collection to it similar by a certain criterion, - it was yesterday.
The index became the key, and the array took shapeassociative array. That is, the key lost its sequential uniqueness (it was usually sequential: 0, 1, 2, ... n) and also became a value, but a simple value (that is, a key) associated with the real value (that is, the content of the element). This is today, it is right, but not perfect.
That is why the PHP foreach loop is considered as an alternative to the usual loop oriented to regular arrays. This is first of all, and this is very important, because it follows from this real correctness of array elements, as well as their keys!
First there was an element, then two elements ... this is how an array of elements appeared and a loop through the array of those:
for ($ i = 0; $ i the 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 the foreach cycle was needed - “a cycle for everyone”: foreach ($ aArrayName as $ xKey => $ xValue) { The processing body of each $ aArrayName [$ xKey] or $ xValue is the same } Now it’s time to get the right elements in the array., that is, those that are in themselves.They themselves know their index, their content, their place in the sequence, tend to show their own choice of sequence and delegate all these possibilities to the array itself, containing them. Such correct arrays will be processed.by themselves. There will simply be no special need to use regular cycles and cycles for each. Formally, the syntax and semantics already allow this, the question is only in the inertia of the developer’s consciousness.