How to search in a PHP array similar to MySQL Like% var% search

Is it possible to search in an array of PHP, as in MySQL.

For example: I have an array

 array( ' mark@test.com '=> `Mark Mian`, ' jhon@test.com '=> `John jack`, ' logon@test.com '=> `Bob Logon`, ' Stela@test.com '=> `Stela Josh`, ' json@test.com '=> `Json Josh` ' bobby@test.com '=> `Bob Mark` ) 

and I would do this type of search,

For example: if I'm looking for Mark , he should return me this

' mark@test.com ' => `Mark Mian

If I'm looking for Bob , he should give me back

' bobby@test.com ' => Bob Mark

' logon@test.com ' => Bob Logon ,

If I search only a , it should return to me those elements that contain a for example:

' mark@test.com ' => Mark Mian ,

' jhon@test.com ' => John jack ,

' Stela@test.com ' => Stela Josh ,

' bobby@test.com ' => Bob Mark

Note. Search must be by key or value

+5
source share
4 answers

Here is a preg_grep solution that should work more like WHERE REGEXP 'PATTERN' in MySQL. I changed the Daniel Klein preg_grep_keys to search for a pattern in the keys of an array and added array_merge to it, which should work with arrays with non-numeric keys. If the keys are numeric, just use a simple preg_grep solution ( preg_grep('~Mark~i', $arr); to find all array elements that have mark or mark , etc.).

array_merge
Combines the elements of one or more arrays together so that the values ​​of one are added to the end of the previous one. It returns the resulting array. If the input arrays have the same string keys, then a later value for this key will overwrite the previous one. If, however, the arrays contain numeric keys, a later value will not overwrite the original value, but will be added.

 function preg_grep_keys_values($pattern, $input, $flags = 0) { return array_merge( array_intersect_key($input, array_flip(preg_grep($pattern, array_keys($input), $flags))), preg_grep($pattern, $input, $flags) ); } $a = array( ' mark@test.by.com '=> "Mark Mian lv", ' jhon@test.lv.com '=> "John jack lv", ' logon@test.en.com '=> "Bob Logon", ' Stela@test.es.com '=> "Stela Josh", ' json@test.es.com '=> "Json Josh", ' bobby@test.lv.com '=> "Bob Mark" ); $r = preg_grep_keys_values('~lv~i', $a); print_r($r); 

Watch this IDEONE Demo

In the above code, lv (case insensitive) is searched for in the keys first, then in the values, and then the results are combined into 1 array. So the results are:

 [ jhon@test.lv.com ] => John jack lv [ bobby@test.lv.com ] => Bob Mark [ mark@test.by.com ] => Mark Mian lv 
+6
source
 $needle="bob"; $output=array(); foreach($array as $k=>$v) { if(stristr($k,$needle) || stristr($v,$needle)) $output[$k]=$v; } print_r($output); 

Fiddle

That is, if you want to search for the keys and values ​​of both, delete part of the keys if you just want to find the values.

+6
source

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); 
+3
source

You want to filter the array, use the array_filter for which it is intended.

If you are only looking for literals, you do not need to use a regex:

 $needle = 'bob'; $result = array_filter($data, function ($k, $v) use ($needle) { return stripos($k, $needle) !== false || stripos($v, $needle) !== false; }, ARRAY_FILTER_USE_BOTH); 

If you want to be able to filter with a regular expression:

 $pattern = '~n.*e~i'; $result = array_filter($data, function ($k, $v) use ($pattern) { return !empty(preg_grep($pattern, [$k, $v])); }, ARRAY_FILTER_USE_BOTH); 
+2
source

Source: https://habr.com/ru/post/1245305/


All Articles