在搜索页面功能的 while 循环中继续语句
Continue statement inside while loop for search page feature
我不明白为什么这个循环不起作用。
此循环的目的只是跳过数组中不等于给定搜索词(在此示例中为 TN)的每个值(在此示例中为 0-2)。所以输出应该是
TN
不应该
IN
TN
OH
这是我的代码:
<?php
$states = array('IN', 'TN', 'OH');
$search = 'TN';
$count = 0;
while($count <= count($states)){
if($states[$count] != $search){
continue;
}
echo $states[$count]."<br/>";
$count++;
}
?>
移动 count++,使其始终递增并保持乐观:
while($count < count($states)){
if($states[$count] == $search){
echo $states[$count]."<br/>";
}
$count++;
}
在数组中搜索值 "TN" 和 return 其键
<?php
$states = array('IN', 'TN', 'OH');
$search = 'TN';
$index = array_search($search, $states);
if($index && $states[$index])
{
echo $states[$index];
}
?>
array_search 是最适合您的解决方案
<?php
$states = array('IN', 'TN', 'OH');
$search = 'TN';
$searcharr = array_search($search, $states);
var_dump($searcharr);
您陷入了无限循环。
1st iteration while 0 < 3, if $states[0] != 'TN' continue to next
iteration $count still 0
2nd iterantion while 0 < 3, if $states[0] != 'TN' continue to next
iteration $count still 0
....... ....... .......
And it continues forever You stuck yourself in checking against 0 index.
您应该在找到匹配项后使用 break 退出 while 循环,而不是递增计数。同样在使用 continue
之前,您应该增加 $count
变量。此外,您的 while 循环条件应该是 <
而不是 <=
因为 count
returns 3,而您的 $states
数组索引是 0,1,2
$states = array('IN', 'TN', 'OH');
$search = 'TN';
$count = 0;
while($count < count($states)){
if($states[$count] != $search){
$count++;
continue;
}
echo $states[$count]."<br/>";
break;
}
而且您可以检查 array_search & in_array 在数组中查找元素,而不是重新发明轮子。
我认为 foreach 循环是比 while 循环更好的解决方案。它不需要 cointinue
或 break
点,也不需要计数来遍历数组。
while 循环在这种情况下似乎没有用。
即:
foreach ($states as $state) {
if ($state == $search) {
echo $state."<br />";
}
}
输出:
TN
但最好的解决方案仍然是 array_search()
。
希望对您有所帮助。
我不明白为什么这个循环不起作用。
此循环的目的只是跳过数组中不等于给定搜索词(在此示例中为 TN)的每个值(在此示例中为 0-2)。所以输出应该是
TN
不应该
IN
TN
OH
这是我的代码:
<?php
$states = array('IN', 'TN', 'OH');
$search = 'TN';
$count = 0;
while($count <= count($states)){
if($states[$count] != $search){
continue;
}
echo $states[$count]."<br/>";
$count++;
}
?>
移动 count++,使其始终递增并保持乐观:
while($count < count($states)){
if($states[$count] == $search){
echo $states[$count]."<br/>";
}
$count++;
}
在数组中搜索值 "TN" 和 return 其键
<?php
$states = array('IN', 'TN', 'OH');
$search = 'TN';
$index = array_search($search, $states);
if($index && $states[$index])
{
echo $states[$index];
}
?>
array_search 是最适合您的解决方案
<?php
$states = array('IN', 'TN', 'OH');
$search = 'TN';
$searcharr = array_search($search, $states);
var_dump($searcharr);
您陷入了无限循环。
1st iteration while 0 < 3, if $states[0] != 'TN' continue to next iteration $count still 0
2nd iterantion while 0 < 3, if $states[0] != 'TN' continue to next iteration $count still 0
....... ....... .......
And it continues forever You stuck yourself in checking against 0 index.
您应该在找到匹配项后使用 break 退出 while 循环,而不是递增计数。同样在使用 continue
之前,您应该增加 $count
变量。此外,您的 while 循环条件应该是 <
而不是 <=
因为 count
returns 3,而您的 $states
数组索引是 0,1,2
$states = array('IN', 'TN', 'OH');
$search = 'TN';
$count = 0;
while($count < count($states)){
if($states[$count] != $search){
$count++;
continue;
}
echo $states[$count]."<br/>";
break;
}
而且您可以检查 array_search & in_array 在数组中查找元素,而不是重新发明轮子。
我认为 foreach 循环是比 while 循环更好的解决方案。它不需要 cointinue
或 break
点,也不需要计数来遍历数组。
while 循环在这种情况下似乎没有用。
即:
foreach ($states as $state) {
if ($state == $search) {
echo $state."<br />";
}
}
输出:
TN
但最好的解决方案仍然是 array_search()
。
希望对您有所帮助。