1.2.1135. Simplify Foreach¶
Remove all unused keys or values from a foreach() call. This prevents PHP from assigning them for nothing, and save some processing time. This is a micro-optimisation.
<?php
// No key usage
foreach($array as $key => $value) {
echo $value;
}
// No value usage
foreach($array as $key => $value) {
echo $key;
}
?>
1.2.1135.1. Connex PHP features¶
1.2.1135.1.1. Suggestions¶
Use array_keys() or array_values() to simplify the source array
Remove the call to $key => when the key is not used in the loop
1.2.1135.1.2. Specs¶
Short name |
Performances/SimplifyForeach |
Rulesets |
|
Exakat since |
2.3.7 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
High |
Available in |