An easy approach would be to use array_filter
If you want regex, this will work
$regex = '~test~'; $result = array_filter($data, function($item) use ($regex) { return preg_match($regex, $item); });
Or just plain contains a search
$search = 'test'; $result = array_filter($data, function($item) use ($search) { return stristr($value, $search); });
If you need to search for both the key and value, you can add the ARRAY_FILTER_USE_BOTH
parameter to array_filter.
$search = 'test'; $result = array_filter($data, function($item, $key) use ($search) { return stristr($value, $search) || stristr($key, $search); }, ARRAY_FILTER_USE_BOTH);
And finally, you can combine array_filter with preg_grep to search both at the same time.
$search = '~bob~i'; $result = array_filter($data, function() use ($search) { return count(preg_grep($search, func_get_args())); }, ARRAY_FILTER_USE_BOTH);
source share