each()
Introduction
As a PHP developer, it is essential to understand the various array functions that PHP provides. One such function is the each() function, which was historically used to iterate over each element of an array and return the key-value pair. Note: each() was deprecated in PHP 7.2 and completely removed in PHP 8.0. This article explains its legacy behavior, syntax, and parameters, and provides modern alternatives.
Syntax
The syntax for the each() function is as follows:
Syntax of Each() function in PHP
each(array $array): array|falseThe each() function takes an array as its parameter and returns an array or false on failure.
Parameters
The each() function takes only one parameter, which is an array. The array can be either indexed or associative. In case of an indexed array, the keys will be the integers starting from 0, and in the case of an associative array, the keys will be the keys that are assigned to each element.
Usage
The each() function is used to iterate over each element of an array and return the key-value pair. This function can be used with both indexed and associative arrays. Each call to each() advances the internal array pointer. The each() function returns an array containing four elements: numeric indices 0 and 1 (aliases for the key and value), and string indices key and value.
Here is an example of how to use the each() function:
Example of how to use the each() function in PHP
<?php
// Legacy PHP 7.x example. This will cause a fatal error in PHP 8.0+.
$array = ["one" => 1, "two" => 2, "three" => 3];
while ($element = each($array)) {
echo $element['key'] . ' => ' . $element['value'] . '<br>';
}In the example above, we first define an associative array with three key-value pairs. Then, we use the each() function to iterate over each element of the array and return the key-value pair. Finally, we use a while loop to print out each key-value pair.
Modern PHP 8+ Equivalent Since each() is no longer available, use foreach for array iteration:
<?php
$array = ["one" => 1, "two" => 2, "three" => 3];
foreach ($array as $key => $value) {
echo $key . ' => ' . $value . '<br>';
}Differences between each() and other similar array functions
The each() function is similar to other array functions like foreach(), array_walk(), and array_map(). However, there are some differences between these functions:
foreach()iterates over each element of an array and assigns the value to a variable. It does not modify the internal array pointer and is the recommended modern approach.array_walk()andarray_map()are used to iterate over an array and apply a user-defined function to each element. They do not return the key-value pair like theeach()function.
Legacy Status & Modern Alternatives
The each() function is deprecated and removed in modern PHP versions. It is no longer recommended for use. The foreach language construct is the standard, safer, and more performant alternative for iterating over arrays in PHP 8+.
Conclusion
In conclusion, the each() function was historically useful for PHP developers to iterate over each element of an array and return the key-value pair. However, due to its removal in PHP 8.0, developers should use foreach for all modern array iteration tasks.
Practice
What is correct about the each() function in PHP according to the content on the provided URL?