Efficient foreach loops: PHP and proper arrays

Table of contents:

Efficient foreach loops: PHP and proper arrays
Efficient foreach loops: PHP and proper arrays

The information presented in arrays can vary in value type and size, and the number of elements cannot always be determined 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 moment in time as a result of various actions or events, in various sequences.

foreach php
foreach php

It is not always possible to predict the operation process at the development stage, to provide for all possible options for the presentation and use of information, the dynamics of their appearance and use.

Syntax for loop by content

In formulating the foreach syntax, PHP offered two options for accessing elements. Both do not depend on the type of the key or the type of the value and can be emulated with a regular loop. It is proposed to consider an array as a collection 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 removed from an array, keys can beassociative and formed by default.

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

This construct forces the PHP foreach loop to go through all the elements in a row. In the body of the loop, the $xValue variable will sequentially receive all the values of the $aArrayName array in the order in which they were added. Item key values will not be available.

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

Here, too, executing the foreach construct, PHP will look at the entire contents of the array, but in the loop body, both the $xValue variable and the $xKey variable, the element key, will take the corresponding values in pairs.

php foreach loop
php foreach loop

Sequence of elements

Inside foreach PHP will offer the content in the order in which the elements were added, but if there were repeated additions/deletions during the formation of the array, and something was added with keys, and something without, then it is best to do working with an array not from the positions of the sequence of elements, but based on their content or on the keys.

php foreach array
php foreach array

Due to various objective reasons, the sequence within the array may not be observed and/or may not be of particular importance, but it should not be relied upon 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 affect the process of creating / editing an array, one should focus oncontent.

Modern "correct" elements

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

The practice when there is a given, and the given has an index in the general collection of similar ones according to a certain criterion - that was yesterday.

The index has become the key, and the array has become 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 (i.e. key) associated with a real value (i.e. the content of the element). It's today, it's right, but not perfect.

This is why PHP sees the foreach loop as an alternative to the regular array oriented loop. This is first of all, and this is what is very important, because the real correctness of the elements of the array follows from this, as well as their keys!

Correct arrays of correct elements

First there was an element, then two elements… so an array of elements appeared and a loop through the array of them:

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 - "loop for each":

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 themselves. They themselves know their index, their content, their place in the sequence, they tend to show their own choice of sequence and delegate all these capabilities to the actual array that contains them.

Such regular arrays will be processed by themselves. There will simply not be a special need to use ordinary cycles and cycles for each. Formally, the syntax and semantics already allow this, the only question is the inertia of the developer's consciousness.

Popular topic

Editor's choice

  • How to block a person in "Contact"? Protect your page from ill-wishers
    How to block a person in "Contact"? Protect your page from ill-wishers

    The popularity of social networks is increasing every day. This is due to the fact that such an instrument of mass communication has entered everyday life and firmly established itself in it, as one of the most convenient ways of communication. Almost every advanced person has his own page on the social network, where he posts his photos, posts posts and, most importantly, communicates with friends and makes new acquaintances

  • Why doesn't "VK" work? The VKontakte website is not working: what to do?
    Why doesn't "VK" work? The VKontakte website is not working: what to do?

    Social media addiction is considered one of the most serious cyber diseases. Every day, tens of millions of users around the globe visit their personal accounts, chatting with friends, discussing the latest events in the world. Social networks have become such a dense part of our lives that it seems unusual to spend a day without looking at them even for a second

  • What is button accordion? Learn slang online
    What is button accordion? Learn slang online

    The Internet is a territory with its own rules, language, style of communication, and, more recently, laws. It so happened that many new users may be left at a loss after communicating with hardened Internet - "hacks". We are talking, first of all, about the use of slang by the latter in their language, which is far from accessible to everyone who opened the page of any forum for the first time

  • Hashtag - what is it? How to use hashtags
    Hashtag - what is it? How to use hashtags

    The Internet is constantly progressing, and every day new features appear in it that make working on it more convenient. They are associated, as a rule, with the optimization and structuring of information, since there is such a large amount of it on the World Wide Web that it is sometimes difficult to find the necessary material. For example, in the recent past, so-called hashtags appeared

  • Why is the internet slow? The most important reasons
    Why is the internet slow? The most important reasons

    In the last couple of years, the Internet has become an important part of the life of almost every person. Using the World Wide Web, we can find out the weather, the latest world news, download the necessary programs, play online games, etc. When the Internet is working well, doing the above things is very nice, but what if all of a sudden web pages start to load for a very long time, and online games slow down?