PHP code:
$search = "Who is KMichaele test";
$array = ["john","michael","adam"];
if (in_array($search, $array)) {
echo "success";
}
else
echo "fail";
I want the success output. How is it possible?
PHP code:
$search = "Who is KMichaele test";
$array = ["john","michael","adam"];
if (in_array($search, $array)) {
echo "success";
}
else
echo "fail";
I want the success output. How is it possible?
You can use array_reduce
and stripos
to check all the values in $array
to see if they are present in $search
in a case-insensitive manner:
$search = "Who is KMichaele test";
$array = ["john","michael","adam"];
if (array_reduce($array,
function ($c, $i) use ($search) {
return $c || (stripos($search, $i) !== false);
},
false))
echo "success";
else
echo "fail";
Output:
success
Edit
Since this is probably more useful wrapped in a function, here's how to do that:
$search = "Who is KMichaele test";
$array = ["john","michael","adam"];
function search($array, $search) {
return array_reduce($array,
function ($c, $i) use ($search) {
return $c || (stripos($search, $i) !== false);
},
false);
}
if (search($array, $search))
echo "success";
else
echo "fail";
$search2 = "michael";
if (search($array, $search2))
echo "success";
else
echo "fail";
Output
success
success