5 Ways To PHP Loop Through Array

5 Ways To PHP Loop Through Array

Hi Dev,

In this tutorial, we are learning 5 types of PHP loop through array. most popular many programming languages have multiple ways to loop through arrays.

The most popular ways 3 types of array like while, for, and foreach loop, but, believe there are more ways to do it with PHP. In this article, I will walk-through each possibility for reading arrays of the loop.

Contents

  • While Loop
  • For Loop
  • Foreach Loop
  • Do While Loop
  • Array Iterator

1. While Loop

While loop is the most popular for loop PHP. This loop recognizable and meaningful name. I always like to the PHP while loop like the following.

While loop is a simple PHP array, we can create a true or false value in PHP arrays depending on if the array has been looping over till the end. To achieve this another variable must be created, an index indicator in the variable that does two things. While loop allows each of the values in the PHP array as well as instructing the while loop when to stop the loop. It’s somewhat very similar to how a for loop works.

PHP Code

 $nameArray = ["Html", "PHP", "CSS", "React", "Angular", "Node"];
        $arrayLength = count($nameArray );
        
        $i = 0;
        while ($i < $arrayLength)
        {
            echo $nameArray [$i] ."<br />";
            $i++;
        }

Output

Html
PHP
CSS
React
Angular
Node

2. For Loop

For loop is a demonstrated in the while loops, you will see how similar the for loop. It uses a same come concept for looping and picking out information form the array. This loop three parameter are needed for the for loop, they are as follows –

Three parameter of For Loop

  1. An initial counter set to a certain value, usually zero.
  2. Boolean test, usually involving the initial counter.
  3. A counter increment eg: counter++.

The start of the for loop will always look similar to the following –

for ($i = 0; $i < count($arr); $i++){
........
}

For loop very important to note here is that you must use semi-colons ( ; ) then, after the first two parameters otherwise the code will throw an exception. there is no need to increment the counter within the loop, as the for loop operator does the for you. let’sput the loop into action in the following PHP.

PHP Code

$nameArray = ["Html", "PHP", "CSS", "React", "Angular", "Node"];
        
      for ($i = 0; $i < count($nameArray ); $i++)  {
            echo $nameArray [$i] ."<br />";
        }

Output

Html
PHP
CSS
React
Angular
Node

3. Foreach Loop

This Foreach loop is my personal favorite print array to read PHP arrays, this loop no need for a boolean text, just simply pass in the PHP print array and do what you want with it. Foreach loop it’s both easy to use and understand and comes in handy for many use cases. Foreach loop isn’t mandatory to use numeric data values, Foreach loop essentially a concept for you easy to use. see it in action –

foreach ($Arrays as $Array)  {
            ......
         }

PHP Code

$nameArray = ["Html", "PHP", "CSS", "React", "Angular", "Node"];
         
       foreach ($nameArray as $value)  {
            echo $value."<br />";
        }

Output

Html
PHP
CSS
React
Angular
Node

4. Do While Loop

Do while loop, you could not almost say loop is pretty scarce among this loop, I personally not use this loop and similarly rarely see from other peoples use this code. It’s essentially a longer of code way of while loop, which in a way, is pretty pointless do while loop. At the same time, it’s arguably more readable, but ultimately, it all comes down to preference for any project. This loop rarely use PHP arrays.

This do while loop, you will need to create a test within the while operator, just as you would in a pure while loop case sees below.

do {
             echo $array[$i] . "
";
             $i++;
         }
         while ($i < count($array));

PHP Code

$nameArray = ["Html", "PHP", "CSS", "React", "Angular", "Node"];
$i = 0;
        
        do {
            echo $nameArray[$i] . "<br />";
            $i++;
        }
        while ($i < count($nameArray));

Output

Html
PHP
CSS
React
Angular
Node

As you can see in the code, there is a need to create an increment index value from the array.

5. Array Iterator

This loop is a slightly more advanced method of looping over arrays is call an Arraylterator. It’s part of wider class that exposes many accessible variables and functions. This loop Arraylterator is more likely to see this part of PHP class and heavily object-orinted projects. This functionality, you many find it more preferable to other vice versa.

This loop ( ArrayIterator )is well worth eye-balling as you can see exactly what benefits it offers. For example, it instantly exposes a count of the array.

PHP Code

$nameArray = ["Html", "PHP", "CSS", "React", "Angular", "Node"];
        
        $arrObject = new ArrayObject($nameArray );
        $arrayIterator = $arrObject->getIterator();

        while( $arrayIterator->valid() )
        {
            echo $arrayIterator->current() . "<br />";
            $arrayIterator->next();
        }

Output

Html
PHP
CSS
React
Angular
Node

References

All loops In some cases, one will perform better than the other with regards to what you need to achieve. I hope this comes in useful all loop, now go do some cool stuff with some loops! Finally, feel free to copy in this article to use in your own projects.

Leave a Reply

Your email address will not be published. Required fields are marked *