【问题标题】:Why doesn't this for loop seem to execute although the condition is still true?尽管条件仍然成立,为什么这个 for 循环似乎没有执行?
【发布时间】:2016-02-11 00:20:16
【问题描述】:

在下面的 SSCCE 中,为什么 for 循环不为 $a 执行大于 3,尽管条件应该让它执行直到 $a 变为 5。

而最后一条语句的输出就更奇怪了。

我想要实现的是,我想删除元素/变量word 具有Select one 值的元素,这样生成的数组是:

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] => ) ) 

【问题讨论】:

  • 这行有问题echo '&lt;br&gt;YES if ( ($items['.$a.']-&gt;word)=="Select one" ) AT '.$a.' INDEX.&lt;br&gt;';//check
  • @Saty 是的。它只会打印到 $a 的值变为 3。我正在编辑问题以发布示例的完整输出。
  • 试试unset($items[$a]-&gt;word);

标签: php for-loop unset array-splice array-unset


【解决方案1】:

条件并不总是正确的。 for 循环中的条件会在每次迭代中重新计算数组的大小。每当删除一个项目时,数组的长度就会发生变化。

每次检查条件时$acount($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

【讨论】:

    【解决方案2】:

    使用temp 变量进行迭代,因为您在 for 执行期间取消设置 main 的键。也许,这应该可行:-

    $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
    }
    

    【讨论】:

    • 这将引发错误,因为 array_splice 不保留数字键。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    相关资源
    最近更新 更多