I've a multidimensional array. I want to search in array using single or double word inside it and return the array's that contain any matched word...
Here is my example array:
$array = array(
array('First_name' => 'Dina', 'Last_Name' => 'Gomez', 'Location' => 'Los Angeles, CA', 'Work' => 'Youtube Service')
array('First_name' => 'Lopa', 'Last_Name' => 'Mitchel', 'Location' => 'New York, NY', 'Work' => 'Works with Mark')
array('First_name' => 'Mark ', 'Last_Name' => 'Nailson Jr.', 'Location' => 'Dallas, USA', 'Work' => 'SEO Industry')
array('First_name' => 'Jenna', 'Last_Name' => 'Gomez', 'Location' => 'Florida, CA', 'Work' => 'Work at Youtube')
);
Now If someone search "Gomez" then it should return 1st and last array..I can do it using in_array() but problem is this, I in_array() only return exact match. I want partial match content too.. like if someone search "Mark Gomez" then it should return 1st array, last array for "Gomez" and 2nd & third array for "Mark" .. 2nd array has "Mark" word at Work Key.
I collect a function from another stackoverflow answer.. which is only work for exact match.. or for some key's only..
function checkarrayvalues($term, $arr, $strict = false, $partial = false) {
if ($partial) { // whether it should perform a partial match
$fn = ($strict) ? "strpos" : "stripos";
}
foreach ($arr as $item) {
if (is_array($item)) {
if (checkarrayvalues($term, $item, $strict, $partial))
return true;
} elseif (($partial && call_user_func($fn, $item, $term) !== false)
|| ($strict ? $item === $term : $item == $term)) {
return true;
}
}
return false;
}
var_dump(checkarrayvalues($query, $results, false, true));
I also tried array_search function :( No luck.
Please help :(
checkarrayvalues()returns a boolean. Do you want to return data or a boolean?