$r = "";
$j = 0;
foreach ($orgs as $k => $v) {
echo "\n\r" . $v->id . "\n\r";
if (1) { //you don't really need this, because it's allways true
$a_view_modl = ArticleView :: model($v->id, $v->id);
$connection = $a_view_modl->getDbConnection();
如果$orgs
是一個關聯數組開始更換$v
重寫代碼,就變成:
$r = "";
$j = 0;
for($i = 0; $i < count($orgs); $i++)
{
echo "\n\r" . $orgs[$i]->id . "\n\r";
$a_view_modl = ArticleView :: model($orgs[$i]->id, $orgs[$i]->id);
$connection = $a_view_modl->getDbConnection();
}
更好的是你先做一些檢查,如果你去這個解決方案。
如果實現與foreach
這是在這種情況下,更可讀的解決方案,你可以增加或減少給定的變量,像正常的:
$i++; //if you want the increment afterwards
++$i; //if you want the increment before you read your variable
同爲遞減:
$i--; //decrement after reading the variable
--$i; //decrement before you read the variable
你需要什麼? – 2014-11-25 12:45:07
爲什麼這需要成爲for循環?你仍然可以在foreach循環的每次迭代中遞增變量 – andrew 2014-11-25 12:45:44
[How'foreach'actually works]的可能重複(http://stackoverflow.com/questions/10057671/how-foreach-actually-works) – 2014-11-25 12:46:03