如果我有阵列:
$os = array("Mac", "NT", "Irix", "Linux");
我知道如果我想在in_array()
内找到"Mac"
,$os
即可使用。
但我有阵列:
$os = array( [1] => "Mac/OSX", [2] => "PC/Windows" );
我想知道"Mac"
中是否包含$os
?
答案 0 :(得分:2)
尝试:
$example = array("Mac/OSX","PC/Windows" );
$searchword = 'Mac';
$matches = array_filter($example, function($var) use ($searchword) { return preg_match("/\b$searchword\b/i", $var); });
答案 1 :(得分:1)
你可以尝试这个 -
$os = array( "Mac/OSX", "PC/Windows" );
function findInArray($os){
foreach($os as $val){
if(strpos($val, $word_to_search) !== false){
return true;
}
}
return false;
}
答案 2 :(得分:1)
您也可以使用array_map来执行此操作。看看下面的代码:
$array = array(
'Mac/OSX',
'PC/Windows',
);
$result = in_array(true, array_map(function ($word, $match, $char = "/") {
$words = explode('/', $word);
return in_array($match, $words) ? true : false;
}, $array, array('Mac')));
var_dump($result); // bool(true)
答案 3 :(得分:0)
这是另一种解决方案:
public class CustomListView extends ListView{
@Override
protected void dispatchDraw(Canvas canvas) {
try {
super.dispatchDraw(canvas);
} catch (IndexOutOfBoundsException e) {
Log.e("luna", "Ignore list view error ->" + e.toString());
}
}
}
答案 4 :(得分:0)
你可以简单地使用PHP的preg_grep
功能,如
$os = array( '1' => "Mac/OSX", '2' => "PC/Windows" );
print_R(preg_grep("/Mac/",$os));
输出:
Array ( [1] => Mac/OSX )
答案 5 :(得分:0)
使用foreach
和strpos
$os =array("Mac/OSX","PC/Windows" );
$string = "Mac";
foreach ($os as $data) {
//echo $data;
if (strpos($data,$string ) !== FALSE) {
echo "Match found";
}else{
echo "not found";
}
}
<强> DEMO 强>
答案 6 :(得分:0)
function FindString($string, $os)
{
// put the string in between //
$preg = "/$string/";
// Match found
$found = false;
// loop each value
for($j = 0; $j < count($os); $j++)
{
// check with pattern
if(preg_match($preg,$os[$j]))
{
// set var to ture
$found = true;
// Break
break;
}
}
if($found == false)
{
die("Unable to found the string $string.");
}
echo "String $string found in array index $j and value is $os[$j]";
}
$where =array("Mac/OSX","PC/Windows" );
$what = "Mac";
FindString($what, $where);