'PHP each() function replacement

I read a textbook and it said each() function is deprecated. The author has recommend his own replacement for each() function called myEach() as following:

function myEach(&$array)
{
    $key = key($array);
    $result = ($key === null) ? false :
              [$key, current($array), 'key', 'value' => current($array)];
    next($array);
    return $result;
}

Is the part: [$key, current($array), 'key', 'value' => current($array)]; wrong?



Solution 1:[1]

The author is correct as each is deprecated : https://www.php.net/manual/en/function.each.php

The 3rd argument is wrong, it should be key => $key

[$key, current($array), 'key' => $key, 'value' => current($array)];

Solution 2:[2]

The function is function myEach(&$array), but myEach(&$array) is not each($array) . I tried to applicate it in a Typesettercms-plugin with an error , probably because &$array is not $array)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thanh Trung
Solution 2