[英]Why doesn't this for loop seem to execute although the condition is still true?
在下面的 SSCCE 中,為什么不for
大於 3 的$a
執行for
循環,盡管條件應該讓它執行直到$a
變為 5。
而最后一條語句的輸出就更奇怪了。
我想要實現的是,我想刪除具有Select one
value for the element/variable word
的元素,這樣生成的數組是:
Array ( [0] => stdClass Object ( [word] => alpha [sentence] => A is the first letter in the word Alpha. ) [1] => stdClass Object ( [word] => beta [sentence] => B is the first letter in the word Beta. ) )
問題是什么把它搞砸了,我能做些什么來解決它?
<?php
$objectOne = new stdClass;
$objectOne->word = 'alpha';
$objectOne->sentence = 'A is the first letter in the word Alpha.';
$objectTwo = new stdClass;
$objectTwo->word = 'beta';
$objectTwo->sentence = 'B is the first letter in the word Beta.';
$objectThree = new stdClass;
$objectThree->word = 'Select one';
$objectThree->sentence = '';
$items = array($objectOne, $objectTwo, $objectThree, $objectThree, $objectThree, $objectThree );
print_r($items);//check
echo '<br><br>count($items) >> '.count($items).'<br><br>';//check
for ($a=0; $a < count($items); $a++) {
echo '<br><br>We are entering index '.$a.'<br><br>';//check
echo '<br>'.$items[$a]->word.'<br>';//check
if ( ($items[$a]->word)=="Select one" ) {
echo '<br>YES if ( ($items['.$a.']->word)=="Select one" ) AT '.$a.' INDEX.<br>';//check
unset($items[$a]);
/**/array_splice($items, $a, 1);
}
echo '<br><br>We are leaving index '.$a.'<br><br>';//check
}
echo '<br><br>AFTER:-<br>';//check
print_r($items);//check
?>
輸出:
Array ( [0] => stdClass Object ( [word] => alpha [sentence] => A is the first letter in the word Alpha. ) [1] => stdClass Object ( [word] => beta [sentence] => B is the first letter in the word Beta. ) [2] => stdClass Object ( [word] => Select one [sentence] => ) [3] => stdClass Object ( [word] => Select one [sentence] => ) [4] => stdClass Object ( [word] => Select one [sentence] => ) [5] => stdClass Object ( [word] => Select one [sentence] => ) )
count($items) >> 6
We are entering index 0
alpha
We are leaving index 0
We are entering index 1
beta
We are leaving index 1
We are entering index 2
Select one
YES if ( ($items[2]->word)=="Select one" ) AT 2 INDEX.
We are leaving index 2
We are entering index 3
Select one
YES if ( ($items[3]->word)=="Select one" ) AT 3 INDEX.
We are leaving index 3
AFTER:-
Array ( [0] => stdClass Object ( [word] => alpha [sentence] => A is the first letter in the word Alpha. ) [1] => stdClass Object ( [word] => beta [sentence] => B is the first letter in the word Beta. ) [2] => stdClass Object ( [word] => Select one [sentence] => ) )
在 for 執行期間取消設置 main 的鍵時,使用temp
變量進行迭代。 也許,這應該有效:-
$temp = $items;
for ($a=0; $a < count($temp); $a++) {
echo '<br><br>We are entering index '.$a.'<br><br>';//check
echo '<br>'.$items[$a]->word.'<br>';//check
if ( ($items[$a]->word)=="Select one" ) {
echo '<br>YES if ( ($items['.$a.']->word)=="Select one" ) AT '.$a.' INDEX.<br>';//check
unset($items[$a]);
/**/array_splice($items, $a, 1);
}
echo '<br><br>We are leaving index '.$a.'<br><br>';//check
}
條件並不總是正確的。 for 循環中的條件在每次迭代中重新計算數組的大小。 每當刪除項目時,數組的長度就會發生變化。
每次檢查條件時$a
和count($items)
的值如下:
$a | count($items) | $a < count($items)
---------------------------------------
0 | 6 | true
1 | 6 | true
2 | 6 | true
3 | 5 | true -- $items[2] was removed
4 | 4 | false -- $items[3] was removed
您應該將數組的大小存儲在變量中並使用它。 此外,由於array_splice
不保留數字鍵,您最終會在嘗試訪問$items[4]
和$items[5]
時收到未定義的偏移通知。 那條線不是必需的。
$count = count($items);
for ($a=0; $a < $count; $a++) {
更好的是,您可以使用foreach
代替for
並使用$item
代替$items[$a]
:
foreach ($items as $a=>$item) {
echo '<br><br>We are entering index '.$a.'<br><br>';//check
echo '<br>'.$item->word.'<br>';//check
...
unset($items[$a]); //can't use $item because it is a copy and not a reference
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.