Innovate faster and cut risk with PHP experts from Zend Services.
Explore Services
See How Zend Helps Leading Hosting Providers Keep Their Managed Sites on Secure PHP
Read More
Learn PHP from PHP experts with free, on-demand, and instructor led courses.
Explore Training
Submit support requests and browse self-service resources.
Explore Support
PHPCompatibility.FunctionUse.RemovedFunctions.eachDeprecated
Replace all usages with this Compatibility_Php56::each() method:
Compatibility_Php56::each()
final class Compatibility_Php56 { public static function each(array &$array) { if (current($array) === false) { return false; } $key = key($array); $value = current($array); next($array); return [ 1 => $value, 'value' => $value, 0 => $key, 'key' => $key, ]; } }
list($key, $value) = each($array);
list($key, $value) = Compatibility_Php56::each($array);
Compatibility_Php56::each() behaves like its deprecated counterpart, each(), which even preserves the ability to access the result's elements using both numeric and string keys (1, value, 0 and key).
each()
1
value
0
key
The reason why we can't just use foreach is because that would not work in situations where we need to both get the values and advance the array's pointer in one expression. Example:
foreach
for ($i = 0; $i < $argLen; $i++) { if ($i + 1 < $argLen) { //... break; } else if (list(, $optArg) = each($args)) { } else { throw new Exception(''); } }
In this situation, if we don't advance the pointer, we'd break the loop's logic. We can't advance the pointer inside the else if, because the pointer has to advance regardless of whether the expression evaluates to true. Replacing it with Compatibility_Php56::each($args) avoids lengthy case-by-case analysis and having to write unit tests for potentially untestable code.
else if
Compatibility_Php56::each($args)
Follow the guide PSR-0 Autoloading to load the Compatibility_Php56 into the legacy project.
Compatibility_Php56
Increase security and cut risk by simplifying PHP application upgrades and migrations.
Long Term Support buys organizations time to migrate.