Search code examples
phparraysstrposarray-filter

how can i filter an array and return matches


I want to return items that are "like" something else. I tried array_filter, but I can't use it correctly.

Here is what I tried. desired out put is

one.php2000565, one.php999.php . Array([0] => one.php2000565[1] => two.php[2] => three.php[3] => one.php999.php[4] => four.php)

$search_text = 'one.php';
array_filter($array, function($a) use ($search_text) {
    return ( strpos($a, $search_text) !== false );
});

Array([0] => one.php2000565[1] => two.php[2] => three.php[3] => one.php999.php[4] => four.php)

$search_text = 'one.php';
array_filter($array, function($a) use ($search_text) {
     return ( strpos($a, $search_text) !== false );
});

Solution

  • $res = array_filter($files, function($files) use ($search_program) {
        return ( strpos($files, $search_program) !== false );
    });
    print_r($res);