Does PHP Have a "built-in" iterator in a Foreach loop?
foreach, iterator, php, phpexcel
Solution
PHP's foreach does not have this functionality built in (per the manual). Use a for loop to have an iterator or implement it yourself.
Problem
I'm using a foreach loop to go through the REQUEST array, as I want to have an easy way to utilize the REQUEST array's keys and values. However, I also want to have a numerical index of how many times the loop has run, as I'm writing a spreadsheet with PHPExcel, and I want to use the `SetCellValue` function. I'm thinking something like this: ``` foreach( $_REQUEST as $key => $value){ $prettyKeys = ucwords(preg_replace($patt_underscore," ",preg_replace($patt_CC,"_",$key))); $prettyVals = ucwords(preg_replace($patt_underscore," ",preg_replace($patt_CC,"_",$value))); // Replace CamelCase with Underscores, then replace the underscores with spaces and then capitalize string // "example_badUsageOfWhatever" ==> "Example Bad Usage Of Whatever" $myExcelSheet->getActiveSheet()->SetCellValue( "A". $built-in-foreach-loop-numerical-index ,$prettyKeys); $myExcelSheet->getActiveSheet()->SetCellValue( "B". $built-in-foreach-loop-numerical-index ,$prettyVals); } ``` I know I can easily implement something like `$c = 0` outsite the foreach and then just increment it each time the loop is run, but is there something cleaner?