I would like to filter (search) a multidimensional array by a search term. I don't want strict identity of the search term to the keys or values but rather more like a case-insensitive contains.
The data in JSON looks like the following:
[
{"title":"The Call of the Wild","author":"Jack London"},
{"title":"Great Expectations","author":"Charles Dickens"},
{"title":"The Voyage of the Beatle","author":"Charles Darwin"}
]
I would like to be able to be able to return an array of results based on the search. For example, a search on the word charles should pull up the second two titles whereas a search on wild should return the first title.
I've been trying to modify the following and answers here but it seems to just give me the index of the array. How can I search on the values for both the title and author of all the elements in the array?
function searchArrayKeyVal($sKey, $search, $array) {
foreach ($array as $key => $val) {
if (strpos(strtolower($val[$sKey]), strtolower(trim($search))) !== false) {
return $key;
}
}
return false;
}
FYI, there is an older version of PHP (5.3) I can't change on my client's host so I can't use newer methods.
Thanks for any suggestions.