在下面SSCCE,为什么没有被超过3 $a
更大的执行for
循环,虽然病情应该让它执行到$a
变得5.为什么这个for循环似乎执行,虽然条件仍然是真的?
而最后一条语句的输出更是怪异。
我想实现的是,我要删除的元素有元素/变量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
?>
OUTPUT:
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] =>))
有你在这一行'回声有问题“
是如果(($项目[” 。$ a。'] - > word)==“选择一个”)AT'。$ a。'指数。
'; //查询 ' – Saty
@Saty Yep。它只会被打印,直到'$ a'的值变为3.我正在编辑问题以发布完整的示例输出。 – Solace
尝试'unset($ items [$ a] - > word);' – Saty