$meal_type= "Free Breakfast|Free Wireless";
if ($meal_type != '' && $meal_type !='None') {
$meal = explode('|', $meal_type);
$meal = array_search('Breakfast',$meal);
$meal = $meal_type;
} else {
$meal= 'No Breakfast';
}
echo $meal;
这是我的代码。在这里我想Breakfast
在数组中搜索并返回搜索到的值,如果找不到return No Breakfast
。
在这里我被| |炸开了要排列的字符串。符号和返回的数组搜索早餐(如果存在)返回资助的数组值,否则回显否早餐值。
一个简单的foreach()
就能完成工作:
<?php
$match_counter =0;
$array = Array
(
0 => 'Free Breakfast',
1 => 'Free Wireless Internet'
);
$search = 'Breakfast';
foreach($array as $arr){
if(stripos($arr,$search) !==false){
echo $arr.PHP_EOL;
$match_counter++;
}
}
if($match_counter ==0){
echo 'No '.$search;
}
输出:-
https://3v4l.org/ogOEB(发现发生)
https://3v4l.org/NTH1W(发现多次)
参考:-stripos()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句