Skip to content

PHP Iterables

Introduction to PHP Iterables

PHP iterables are data structures that allow you to store and manipulate multiple values in a single variable. Arrays are the most common iterable, but PHP also supports objects that implement the Traversable interface. The iterable pseudo-type can be used as a type hint for both arrays and Traversable objects. Array values can be of different data types and can be easily retrieved and processed using various array functions.

Types of Iterables in PHP

Arrays, the most frequently used iterables in PHP, come in two main types: indexed arrays and associative arrays.

An indexed array stores values under a numeric index, starting from 0, while an associative array uses a string as the index, allowing you to access its values by specifying the key.

Creating and Accessing Iterables in PHP

To create an array in PHP, use square brackets followed by a list of values. For example:

PHP define an array

php
$fruits = ["apple", "banana", "cherry"];

To access values in an array, you can use the square bracket notation, with the index of the value you want to access. For example:

PHP print first element of an array

php
echo $fruits[0]; // Output: apple

Array Functions in PHP

PHP provides a wide range of functions that you can use to manipulate and process arrays. Some of the most commonly used array functions include:

  • array_keys: returns an array of all the keys in the input array
  • array_values: returns an array of all the values in the input array
  • sort: sorts the values in an array in ascending order and modifies the array in-place, returning a boolean on success
  • count: returns the number of elements in an array

Working with Associative Arrays in PHP

Associative arrays allow you to access values by specifying a key instead of an index. To create an associative array, use square brackets followed by a list of key-value pairs. For example:

PHP associative arrays example

php
$student = ["name" => "John Doe", "age" => 25, "country" => "USA"];

To access values in an associative array, use the key within square brackets. For example:

PHP access values in an associative array

php
echo $student["name"]; // Output: John Doe

Iterating with foreach

To iterate over an array or any iterable, PHP provides the foreach loop. For example:

PHP iterate over indexed array

php
foreach ($fruits as $fruit) {
    echo $fruit . "\n";
}

For associative arrays, you can access both the key and the value:

PHP iterate over associative array

php
foreach ($student as $key => $value) {
    echo "$key: $value\n";
}

Conclusion

PHP iterables are an essential part of programming in PHP and provide a convenient way to store and manipulate multiple values in a single variable. With a wide range of array functions, the iterable type hint, and support for both indexed and associative arrays, you have the tools you need to efficiently manage your data and build dynamic, powerful applications.

Practice

What can be considered as Iterables in PHP?

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.