[英]Check if any array value contains a string - strpos
為了檢查字符串中是否存在子字符串:
if (strpos($haystack,$needle)!==false) {...}
但是需要檢查任何數組值是否包含字符串。
例如:檢查“ oran”是否包含在$ arr的任何值中
$arr=array('orange1','orange2','orange3')
始終可以使用strpos進行foreach並分析每個數組值。
但是,有一個優雅而又不錯的選擇嗎?
這個怎么樣?
if(strpos(implode(' ', $arr), 'oran') !== false){...}
您需要使用空格分隔符,以免意外:
$arr = array('for', 'antlr'); // implode would make 'forantlr', which contains 'oran'
foreach ($arr as $a) {
if (strpos($a,'oran') !== false) { ... }
}
我知道我們都在想這個。 /線。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.