I have a multidimensional array. They keys of the separate arrays are text. I would like to know how to match the key with part of a string that's from looped SELECT data.
Example of Array
Array ( [Volvo] => Array ( [0] => )
[Ford] => Array ( [0] => )
[Seat] => Array ( [0] => ) )
So from this array $cars
, how would I be able to match data coming from my SELECT
query
Select Output echo $row['car'];
Volvo, Mercedes, Lexus
What I'm after
if the key of one of my arrays matches part of the string from my select query, so as the example says above Volvo would match, then do something. That something for me is adding more data in that array.
I have tried variations of in_array
, array_search
and array_filter
but have not found a solution that works for me.
Example of the reverse of what i was doing through a foreach
loop
$searchword = $array_element;
$result = array_filter($cars, function($var) use ($searchword) { return preg_match("/\b$searchword\b/i", $var); });
taken from: Search for PHP array element containing string