Last active
October 28, 2022 09:47
-
-
Save krmgns/30dd2bf8b22329a2dbc11a045aed3859 to your computer and use it in GitHub Desktop.
Polyfill for "each" function that was removed as of PHP/8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!function_exists('each')) { | |
function each(array &$array) { | |
$value = current($array); | |
$key = key($array); | |
if (is_null($key)) { | |
return false; | |
} | |
// Move pointer. | |
next($array); | |
return array(1 => $value, 'value' => $value, 0 => $key, 'key' => $key); | |
} | |
} | |
/* | |
// Yields same results with the samples on docs: https://php.net/each | |
$foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese"); | |
print_r(each($foo)); | |
$foo = array("Robert" => "Bob", "Seppo" => "Sepi"); | |
print_r(each($foo)); | |
print_r(each($foo)); | |
print_r(each($foo)); // false | |
$fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry'); | |
while (list($key, $val) = each($fruit)) { | |
echo "$key => $val\n"; | |
} | |
*/ |
You should move this into a real composer package.
I already did, see https://github.com/craftgate/craftgate-php-client/pull/14/files
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should move this into a real composer package.