Last active
December 7, 2017 10:44
-
-
Save oschonrock/e14822590d2689769b5b2564ea9dabf0 to your computer and use it in GitHub Desktop.
leach() = "legacy each": php72 compat for deprecated each(). Use to refactor tired third party libs, when not performance critical.
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 | |
/** | |
* leach() = "legacy each" | |
* | |
* replacement for deprecated each() for php7.2 | |
* use this if refactoring would be too painful and performance is not relevant | |
* | |
*/ | |
function leach(&$arr) | |
{ | |
$k = key($arr); | |
if ($k === null) | |
{ | |
// https://secure.php.net/manual/en/function.each.php | |
// If the internal pointer for the array points past the end of | |
// the array contents, each() returns FALSE. | |
return false; | |
} | |
else | |
{ | |
$v = current($arr); | |
// After each() has executed, the array cursor will be left on the | |
// next element of the array, or past the last element if it hits the | |
// end of the array. | |
next($arr); | |
// Returns the current key and value pair from the array | |
// array. This pair is returned in a four-element array, with the | |
// keys 0, 1, key, and value. Elements 0 and key contain the key | |
// name of the array element, and 1 and value contain the data. | |
return [ | |
1 => $v, | |
'value' => $v, | |
0 => $k, | |
'key' => $k | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment