PHP equivalent to Python's enumerate()? PHP equivalent to Python's enumerate()? php php

PHP equivalent to Python's enumerate()?


Don't trust PHP arrays, they are like Python dicts. If you want safe code consider this:

<?php$lst = array('a', 'b', 'c');// Removed a value to prove that keys are preservedunset($lst[1]);// So this wont workforeach ($lst as $i => $val) {        echo "$i $val \n";}echo "\n";// Use array_values to reset the keys insteadforeach (array_values($lst) as $i => $val) {        echo "$i $val \n";}?>

-

0 a 2 c 0 a 1 c 


Use foreach:

foreach ($lst as $i => $val) {    echo $i, $val;}


Yes, you can use foreach loop of PHP:

 foreach($lst as $i => $val)       echo $i.$val;