I know i can do it like this when i'm looking for value inside array.
$example = array('example','One more example','last example');
$searchword = 'last';
$matches = array_filter($example, function($var) use ($searchword) { return preg_match("/\b$searchword\b/i", $var); });
However I'm wanting to do something like this but for this example:
$example = array( "first" => "bar", "second" => "foo", "last example" => "boo");
$searchword = 'last';
How can I change this to get the key value which contains searchword
instead of the value?
$matches = array_filter($example, function($var) use ($searchword) { return preg_match("/\b$searchword\b/i", $var); });
You can try this one. Here we are using
array_flip
,array_keys
andpreg_grep
Solution 1:
Try this code snippet here
Solution 2: (
Since PHP 5.6
) A good recommendation by @axiacTry this code snippet here