|
|
|
|
|
by wackget
2038 days ago
|
|
Where's the best place to suggest a new feature or improvement for PHP? I've always wanted to see `foreach` get a built-in iterator/counter so you don't have to create and use a counter variable manually. Current way: $i=0;
foreach ($array as $key => $value) {
echo "Loop $i of " . count($array);
$i++;
} Possible new way: foreach ($array as $key => $value, $i) {
echo "Loop $i of " . count($array);
} |
|
Is there such a function in php like "enumerate" in python, that yields a tuple of (counter, item) for each item in the first argument to enumerate? That is to say: is it possible to do what you're asking with composition rather than a language change?