使用正则表达式和in_array()在数组中搜索单词

I have array list like:

$arrval[0] = "fulanah bin fulan";
$arrval[1] = "joko bin dodo";
$arrval[3] = "mabhok bin jahannam";

Then, i want to use

in_array()

Or something like that, for search an array with some words, for example using only word "joko". So if found in array list it will return true.

Do i have to use regex?, if so, how is its pattern and usage in function in_array () ?, thanks.

You don't need in_array, preg_grep could handle array well. Try this:

<?php
$arrval[0] = "fulanah bin fulan";
$arrval[1] = "joko bin dodo";
$arrval[2] = "mabhok bin jahannam";

$search_word="joko";
var_dump(preg_grep("/$search_word/",$arrval));
$search_word="test";
var_dump(preg_grep("/$search_word/",$arrval));

You can not do this with in_array(). But you can use preg_grep().

$arrval[0] = "fulanah bin fulan";
$arrval[1] = "joko bin dodo";
$arrval[3] = "mabhok bin jahannam";

var_dump(preg_grep ('/\bjoko\b/', $arrval));

Then you can return

$result = preg_grep ('/\bjoko\b/', $arrval);
return !empty($result);

Output

array(1) {
  [1]=>
  string(13) "joko bin dodo"
}

Demo