Effective foreach loops: PHP and regular arrays

Information presented in arrays can bediffer in the type of values ​​and their size, and the number of elements can not always be determined in advance. Modern programming, especially in the distributed version, allows creating complex data structures whose content and properties can be determined dynamically at an indefinite point in time as a result of various actions or events in a different sequence.

foreach php

Not always at the development stage, you can predict the operation process, provide for all possible options for the presentation and use of information, the dynamics of their appearance and use.

Cycle Syntax by Content

Formulating the syntax of foreach, PHP proposed twooption for accessing items. Both do not depend on either the key type or the value type and can be emulated by the normal loop. It is suggested to consider the array as a collection of elements, the number of which is not initially defined. An array can be formed on the fly, with or without keys. In an array, an element can be deleted, keys can be associative and formed by default.

foreach ($ aArrayName as $ xValue) {body of the loop}

Such a construction requires a foreach loopwalk through all the elements in a row. In the body of the loop, the variable $ xValue will sequentially take all values ​​of the array $ aArrayName in the order in which they were added. Element key values ​​will not be available.

foreach ($ aArrayName as $ xKey => $ xValue) {body of the loop}

Here too, by executing the foreach construct, PHPwill scan the entire contents of the array, but in the body of the loop, the corresponding values ​​will be taken as the variable $ xValue, and the variable $ xKey - the key of the element.

foreach php loop

Sequence of elements

Inside PHP foreach will offer content inIn the order in which the elements were added, but if there were multiple additions / deletes during the array formation, and something was added with the keys, and something without, then it's best to work with the array not from the sequence of elements elements, but based on their content or on the keys.

php foreach array

В силу различных объективных причин the sequence within the array may not be respected and / or may not be of particular importance, but it should not be oriented in any way. In simple problems, on trivial data sets, there are no problems, and the algorithm can be configured for sequential processing, but when the process of creating / editing an array is influenced by many factors, one should focus on the content.

Modern "correct" elements

From the standpoint of the existing own concept, without even taking into account the unconditional similar languages, the PHP foreach array must be designed independently taking into account the real concrete task.

Practice when there is a given, and this one has an index in the general collection of him similar according to a certain criterion - it was yesterday.

The index became the key, and the array took shapeassociative array. That is, the key has lost its sequential uniqueness (it was usually consistent: 0, 1, 2, ... n) and became a value, too, but a simple value (that is, a key) associated with the real value (that is, the content of the element). It's today, it's right, but not perfect.

This is why the foreach loop is considered by PHP as an alternative to a regular loop that is oriented to regular arrays. This is primarily, and this is very important, because it implies real correctness of array elements, as well as their keys!

Correct arrays of regular elements

First there was an element, then two elements ... so there was an array of elements and a loop along an array of those:

for ($ i = 0; $ i <count ($ aArrayName); $ i ++) {

the processing body of each $ aArrayName [$ i]


Then the element instead of the faceless 0, 1, 2, ... n had its own name - the key and then the arrays became associative, and then a foreach loop was needed - a "loop for each":

foreach ($ aArrayName as $ xKey => $ xValue) {

the processing body of each $ aArrayName [$ xKey] or $ xValue that is the same


Now it's time to come to the array with the right elements, that is, those that are on their own. They themselves know their index, their content, their place in sequence, are inclined to exercise their own choice of sequence and delegate all these their possibilities to the actual array that contains them.

Such correct arrays will be processedby themselves. A special need for using ordinary cycles and cycles for each will simply not be. Formally, the syntax and semantics already allow, the question is only the inertia of the developer's consciousness.

  • Rating: