amy burgess amy burgess - 4 years ago 130
PHP Question

how to filter out an array that does not contain from specified string using PHP?

I would like to know how can I filter out an arrray that does not contain from specified sting?

Example:

$array = array('January', 'February', 'March', 'April');
$to_search = "uary"; // not case sensitivity
$result = somefunction($array, $to_search );
print_r($result);


The expected output is:

$result = array('January', 'February');


I hope to use the most efficient and fastest way in searching among the arrays because the array that I might use contains at least 100 items...

I'm not sure how to use the
preg_match
.

Answer Source

DEMO

You should choose array_filter function. e.g.

$to_search = "uary"; // not case sensitivity
$array = array('January', 'February', 'March', 'April');

$result = array_filter($array, function ($item) use ($to_search ) {
 if (stripos($item, $to_search ) !== false) {
    return true;
 }
return false;
});

and to get and display your array, use the var_dump

var_dump($result);

Please try with this and let me know.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download