preg_grep – find keys in an array that match a pattern

Sometimes there is a need to find a key/s of an array that matches a particular pattern. array_search searches the array for a given value and returns the corresponding key but it matches the entire word. Here's a scenario. You have a list of tags and you want to provide a user to seach a particular tag which closely matches to their search. So here are some of the tags in an array [crayon-59ebd6cd49545736338097/] If the user enters Wordpress we want to show Wordpress Plugins as well as Wordpress Tweaks. So this is how it can be done using preg_grep [crayon-59ebd6cd49553844942339/] where $key is the user input i.e. Wordpress in this case In our case $filtered_tags will be an array and will return 0=>Wordpress Plugins 1=>Wordpress Tweaks  ...
Read More