I have this code:
foreach ($items as $key=>$value) {
if (strpos(strtolower($key), $text) !== false) {
array_push($result, array("id"=>$value, "label"=>$key, "value"=>strip_tags($key)));
}
if ( count($result) > 2 )
break;
}
The break statement does not work so I get more than 2 items. However if I change the break; to
die('results more than 2');
it shows that it's working properly.
Am I using the break statement correctly?
Of course you get more than 2 items. You said if count($result) > 2
and not >= 2
So when you have more than 2 results, it breaks.
If you break
with if ( count($result) > 2 )
then of course you'll have more than 2 items. If you want only two, just use if ( count($result) >= 2 )
应用它并测试结果:
if ( count($result) >= 2 ) break;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.