我使用 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
循环将为您提供一个自动计数器,但无法循环遍历您的$_REQUEST
关联数组。 foreach
循环将让您循环遍历,但没有内置计数器。这是一种权衡,但至少这是一个非常易于管理的权衡(只需要 2 行代码即可构建计数器)!
使用SPL迭代器类。我确信里面有一些东西你可以利用。
不,没有内置的迭代器数字索引。不过,您可以通过其他方式解决这个问题。
最明显的方法是使用简单的
for
循环:
for ($i = 0, $numFoo = count($foo); $i < $numFoo; ++$i) {
// ...
}
您还可以将
foreach
与计数器变量一起使用:
$i = 0;
foreach ($foo as $key => $value) {
// ...
++$i;
}
你绝对可以用 for 循环来做到这一点,只是有点难看:
reset($array);
for ($i=0; $i<count($array); $i++) {
// $i is your counter
// Extract current key and value
list($key, $value) = array(key($array), current($array));
// ...body of the loop...
// Move to the next array element
next($array);
}
我的首选方法是扩展 ArrayIterator。添加一个内部计数器,然后重写 next() 和 rewind() 以更新计数器。计数器给出当前数组元素的从 0 开始的键:
class MyArrayIterator extends ArrayIterator
{
private $numericKey = 0;
public function getNumericKey()
{
return $this->numericKey;
}
public function next()
{
$this->numericKey++;
parent::next();
}
public function rewind()
{
$this->numericKey = 0;
parent::rewind();
}
}
// Example:
$it = new MyArrayIterator(array(
'string key' =>'string value',
1 =>'Numeric key value',
));
echo '<pre>';
foreach ($it as $key =>$value) {
print_r(array(
'numericKey' =>$it->getNumericKey(),
'key' =>$key,
'value' =>$value,
));
}
// Output:
// Array
// (
// [numericKey] => 0
// [key] => string key
// [value] => string value
// )
// Array
// (
// [numericKey] => 1
// [key] => 1
// [value] => Numeric key value
// )
您的问题也在这里得到解答:如何在不使用“计数器”变量的情况下找出 PHP 中 foreach 构造循环的次数?
很快,不会。没有更简单的方法了。
可以获取$_REQUEST数组中每个键的索引:
$req_index= array_flip (array_keys($_REQUEST));
现在您通过
$req_index[$key]
获得了当前元素的数字索引,这也给出了循环的迭代次数:
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"
//THE NUMERICAL INDEX IS $req_index[$key]
$myExcelSheet->getActiveSheet()->SetCellValue( "A". $req_index[$key], $prettyKeys);
$myExcelSheet->getActiveSheet()->SetCellValue( "B". $req_index[$key], $prettyVals);
}
您实际上可以将
foreach
与 array_values()
组合使用,这将为您提供数组中每个元素的键。
例如:
$colors = ["red", "blue", "green"];
foreach(array_values($colors) as $key => $color) {
echo "$color $key";
}
/**Output:
red 0
blue 1
green 2*/