1

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.

2
  • Merge 3 arrays and chunk array with space and add into separate array items and then use in_array() Commented Jan 25, 2018 at 4:39
  • @DeadManAlive can we do this in one line ?, like inside in_array() function ? Commented Jan 25, 2018 at 4:42

2 Answers 2

1

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));
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.