问题
This issue is related to following issue: Search multidimensional array in PHP and return keys
I have following array I would like to search for strings using e.g. regular expressions:
[390] => Array
(
    [0] => hammer
    [1] => Properties
    [2] => tools, hammer, properties
    [3] => 
    [4] => done
    [png] => Array
        (
            [0] => hammer_16x.png
            [1] => hammer_32x.png
        )
    [eps] => Array
        (
            [0] => hammer_16x.eps
            [1] => hammer_32x.eps
        )
    [ico] => Array
        (
            [0] => hammer.ico
        )
)
I would like to especially search these values:
 [0] => hammer
 [1] => Properties
 [2] => tools, hammer, properties
 [3] => 
 [4] => done
e.g. the user shall have the possibility to find this array key when searching for "ham", "tools", "amm" etc.
I tried to adapt the solution posted in the post above but did not manage it. I've also found a solution using array_map, but this did not enable me to explicitly search in a specific attribute (e.g. I would like to further on limit a search to the first index in the array, here [0] => hammer):
$result= array_map('unserialize', preg_filter('/'.$searchterm.'/', '$0', array_map('serialize', $array)));
Your ideas are welcome :) Thanks!
回答1:
Try the following solution. It doesn't use regular expressions, it use plain string search instead. However it allows some flexibility as you want.
function filter($array, $filter_elem) {
    return array_filter($array, function ($v) use($filter_elem) {
        return
            count(array_udiff_assoc($filter_elem, $v, function ($a, $b) {
                if (!is_string($a) || !is_string($b)) return $a == $b? 0 : 1;
                return stripos($b, $a) !== false || stripos($a, $b) !== false? 0 : 1;
            })) == 0;
    });
}
$array = array(
    '390' => array(
        '0' => 'hammer',
        '1' => 'Properties',
        '2' => 'tools, hammer, properties',
        '3' => false,
        '4' => 'done',
        'png' => array(
            '0' => 'hammer_16x.png',
            '1' => 'hammer_32x.png',
        ),
        'eps' => array(
            '0' => 'hammer_16x.eps',
            '1' => 'hammer_32x.eps',
        ),
        'ico' => array(
            '0' => 'hammer.ico',
        ),
    ),
);
$filter_elem = array(
    '1' => 'prop',
    '2' => 'hammer',
    '3' => false,
    '4' => 'done',
);
print_r(filter($array, $filter_elem));
回答2:
You may use this recursive function of preg_grep:
function preg_grep_recursive( $pattern, $input, $flags = 0, &$output = array() )
{
    foreach($input as $key => $val) {
        if(is_array($val)) {
            preg_grep_recursive($pattern, $val, $flags, $output);   
        } else {
            $output[] = $val;
        }
    }
    return preg_grep($pattern, $output, $flags);
}
来源:https://stackoverflow.com/questions/22381434/search-multidimensional-array-using-regular-expressions-in-php-and-return-keys