What Did the each() Function Do

The each() function returned the key and value of the current element in an array and moved the internal pointer forward.

$array = ["a" => 1, "b" => 2, "c" => 3];
while ($element = each($array)) {
    print_r($element);
}

Output:

Array ( [1] => 1 [value] => 1 [0] => a [key] => a )
Array ( [1] => 2 [value] => 2 [0] => b [key] => b )
Array ( [1] => 3 [value] => 3 [0] => c [key] => c )

How to Simulate the Each Functionality in PHP Without Built-In Array Functions

In earlier versions of PHP, the each() function was used to iterate through an array, returning both the key and value of the current element. However, each() is now deprecated. This post demonstrates how to replicate its functionality manually without using any PHP array functions.

Simulating each() Without PHP Array Functions

To replicate each(), we need to manually track the current position of an array and fetch the corresponding key-value pair. Here’s how you can do it step by step.

1. Initialize the Array and Manual Pointer

Start by defining your array and a pointer to track the current position.

$array = ["a" => 1, "b" => 2, "c" => 3];
$pointer = 0; // Start at the first index
$keys = array_keys($array); // Extract keys

2. Fetch the Current Element

Manually access the key and value based on the pointer position.

if ($pointer < count($array)) {
    $key = $keys[$pointer];
    $value = $array[$key];
    $element = ["key" => $key, "value" => $value];
    print_r($element);
}

3. Move to the Next Element

Increment the pointer to move to the next element in the array.

$pointer++; // Move to the next element

4. Iterate Over the Array

Use a while loop to mimic the behavior of each().

$array = ["a" => 1, "b" => 2, "c" => 3];
$pointer = 0;
$keys = array_keys($array);

while ($pointer < count($array)) {
    $key = $keys[$pointer];
    $value = $array[$key];
    $element = ["key" => $key, "value" => $value];
    print_r($element);
    $pointer++;
}

Complete Code Example:

$array = ["a" => 1, "b" => 2, "c" => 3];
$pointer = 0; // Initialize manual pointer
$keys = array_keys($array); // Get array keys

// Iterate through the array
while ($pointer < count($array)) {
    $key = $keys[$pointer]; // Get current key
    $value = $array[$key];  // Get current value

    // Simulate `each` output
    $element = [
        "key" => $key,
        "value" => $value
    ];

    print_r($element); // Output the key-value pair
    $pointer++; // Move to the next element
}

Output:

Array ( [key] => a [value] => 1 )
Array ( [key] => b [value] => 2 )
Array ( [key] => c [value] => 3 )

Advantages of Manual Simulation

  1. Custom Control: Provides flexibility to manage the array traversal.
  2. Compatibility: Works in any PHP version without relying on deprecated functions.
  3. Clear Understanding: Helps developers understand the inner workings of array traversal.

Even though the each() function is no longer available, you can easily replicate its behavior manually by tracking the keys and values of an array.

Keep Learning 🙂

Leave a Reply

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