ComputersInformation Technology

Effective foreach loops: PHP and regular arrays

Information represented in arrays can differ in the type of values and in their size, and the number of elements can not always be determined in advance. Modern programming, especially in the distributed version, allows you to create 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.

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 offered two options for accessing elements. 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. The 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}

This design obliges the foreach PHP loop to go through all the elements in a row. In the body of the loop, the $ xValue variable 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) {body of the loop}

Here, too, while executing the foreach construct, PHP will scan the entire contents of the array, but in the body of the loop, the corresponding values will be in pairs, both $ xValue and $ xKey, the element's key.

Sequence of elements

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

Due to various objective reasons, the sequence within the array may not be observed and / or may not have much significance, but it should not be oriented in any case. In simple problems, on trivial data sets, there are no problems, and the algorithm can be tuned to sequential processing, but when the process of creating / editing an array is influenced by many factors, one should focus on the content.

Modern "right" elements

From the standpoint of the existing own concept, without even taking into account the unconditional similarity of languages, PHP foreach array must be designed independently taking into account a 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 on the form of an associative 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 .

That is why the foreach loop is considered by PHP as an alternative to a regular loop, oriented to regular arrays. This is first and foremost, and this is very important, because this implies the real correctness of the elements of the array , 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 these:

For ($ i = 0; $ i

The processing body of each $ aArrayName [$ i]

}

Then the element instead of 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, ones that are on their own. They themselves know their index, their content, their place in the sequence, are inclined to show their own choice of sequence and delegate all these possibilities to the actual array that contains them.

Such regular arrays will be handled by 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.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.atomiyme.com. Theme powered by WordPress.