PHP Array Iterator with Example Program

In this tutorial, we will discuss on how to iterate array in PHP. While iterating across Arrays and Objects, this iterator gives users the ability to edit values and keys as well as unset them.

However, If you wish to iterate over the same array more than once, you need to instantiate ArrayObject and let it construct ArrayIterator instances that refer to it. You may do this by invoking its getIterator() function directly or by using the foreach keyword.

Also, You can visit the other PHP tutorials for more knowledge about programming.

Class synopsis examples

class ArrayIterator implements SeekableIterator, ArrayAccess, Serializable, Countable {
/* Constants */
const int STD_PROP_LIST = 1;
const int ARRAY_AS_PROPS = 2;
/* Methods */
public __construct(array|object $array = [], int $flags = 0)
public append(mixed $value): void
public asort(int $flags = SORT_REGULAR): bool
public count(): int
public current(): mixed
public getArrayCopy(): array
public getFlags(): int
public key(): string|int|null
public ksort(int $flags = SORT_REGULAR): bool
public natcasesort(): bool
public natsort(): bool
public next(): void
public offsetExists(mixed $key): bool
public offsetGet(mixed $key): mixed
public offsetSet(mixed $key, mixed $value): void
public offsetUnset(mixed $key): void
public rewind(): void
public seek(int $offset): void
public serialize(): string
public setFlags(int $flags): void
public uasort(callable $callback): bool
public uksort(callable $callback): bool
public unserialize(string $data): void
public valid(): bool
}

ArrayIterator Flags

NameDescription
ArrayIterator::STD_PROP_LISTWhen the object’s properties are accessed as a list (via var dump, foreach, etc.), they work as they normally do.
ArrayIterator::ARRAY_AS_PROPSProperties can be used to get to entries (read and write).

What is array iterator in PHP?

The PHP 8 Standard Library’s ArrayIterator class makes it easy to go through a list. Now, we can easily go through the objects in an array. The array iterator is another great iterator from the Standard PHP Library, or SPL. We use it when we go over objects over and over.

In PHP, an array is a map with an order. At this point, we don’t have time to talk about PHP data structure. But we can say that with the help of an array or a multidimensional array, we can build a very complicated relationship tree that shows how the keys and values of the different arrays are related.

How can I iterate through an array in PHP?

To go through an array in PHP, we use the foreach loop. But the while loop can also be used with the ArrayIterator class, which implements many other interfaces like Serializable, Countable, etc.

We can start with a simple list of web and mobile programming languages as an example.

WordPress and Flutter are on the list of arrays, even though they are not languages but rather software tool kits. We keep them on the list, though, because they are useful.

For example iterate array PHP:

<?php
            
            $webLanguages= array('PHP', 'WordPress', 'Flutter', 'Dart', 'Python', 'Java', 'ASP.net', 'Django', 'JavaScript');
            $webLanguages= new ArrayIterator($webLanguages);
            $webLanguages->asort();
            
            foreach ($webLanguagesas $webMobileLanguages) {
                echo $webMobileLanguages. '<br><br>';
            }
            
?>

We can turn that simple array into objects by using the ArrayIterator class.

After that, we easily put these things in ascending order using a method called asort ().

Then, we used the foreach loop.

Even though the algorithm is simple, everything has been taken care of by the PHP 8 Standard Library.

The result is the following output.

ASP.net

Dart

Django

Flutter

Java

JavaScript

PHP

Python

WordPress

Which loop would you prefer to go through an array in PHP briefly describe?

If we want to go through an array in PHP, we usually use the foreach loop, but we can also use the while loop.

We can do this with the help of the ArrayIterator class.

Now, we’ll look at an associative array, which is a group of key-value pairs.

Let’s start by taking a look at the code example below for array to iterator PHP. Second, we’ll talk about the code to understand the ArrayIterator class’s main ideas and methods.

<?php
          $languages = array(
              "PHP" => "PHP 8 Standard Library",
              "WordPress" => "WordPress the Coding is Easy and make it simple",
              "Flutter" => "Flutter: The Complete Tutorial for beginners",
              "Dart" => "Dart: The Complete Tutorial for beginners"
          );
          $obj = new ArrayObject( $languages );
          $iterator = $obj->getIterator();

          echo "Iterating over: " . $obj->count() . " values</br></br>";

          echo "<h1>Getting output from While Loop with Key=Value</h1>";

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

          echo "</br></br>";
          echo "<h1>Getting output from forach Loop, only value </h1>";


          foreach ($iterator as $key=>$val)
          echo $key.":".$val."</br></br>";

?>

The languages array is an associative array, which means that each key points to a value.

After that, we just use one line of code to turn that array into objects.

$obj = new ArrayObject( $books );

Next, we can use a method called getIterator to manually go through those book objects ().

It really helps us.

Why?

We can now point the key to the current value as long as this object can be looped over.

Not only that, but we can use the next method to keep going inside the while loop until the loop keeps going.

But using foreach makes it easier.

Summary

In summary, This simple tutorial of PHP iterate through array should have helped you with your programming. We need your help to keep going. Did we forget something about PHP Array Iterator? Please tell us by leaving a comment below.

Leave a Comment