【发布时间】:2012-08-13 14:36:01
【问题描述】:
我正在使用 foreach 循环遍历 REQUEST 数组,因为我希望有一种简单的方法来利用 REQUEST 数组的键和值。
但是,我还想要一个循环运行次数的数字索引,因为我正在使用 PHPExcel 编写电子表格,并且我想使用 SetCellValue 函数。我在想这样的事情:
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);
}
我知道我可以很容易地在 foreach 外部实现类似 $c = 0 的东西,然后每次运行循环时递增它,但是有什么更干净的东西吗?
【问题讨论】:
-
一句话,没有。与几乎任何语言一样,如果你想要一个计数器,你必须自己实现它。
-
如果您需要此功能,请使用
for循环。
标签: php foreach iterator phpexcel