Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)];
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading