I have two arrays which look like the following:
$arr1 = ("stringType1AndSomeRandomStuff",
"stringType2AndSomeRandomStuff",
"stringType3AndSomeRandomStuff",
"stringType1AndSomeRandomStuff",
"stringType2AndSomeRandomStuff",
"i don't belong here at all!",
"stringType4AndSomeRandomStuff");
In this first array ($arr1
), most of the keys have some sort of common attribute. In the example text above, this would be stringTypeX
. This 'common factor' is what I need to search by. Each string also has some sort of extra data exemplified by AndSomeRandomStuff
.
The second array looks like this:
$arr2 = ("stringType1" => "category1",
"stringType2" => "category2",
"stringType3" => "category3",
"stringType4" => "category4");
I need to go through each string in $arr1
and see if it closely matches any of the keys in $arr2
. If it matches for one of the keys, I need the value of the key from $arr2
.
How can I iterate through each of the strings in $arr1
and determine which (if any) of the keys in $arr2
apply? Basically, I need to go through every string in $arr1
and perform a partial match on all of the keys in $arr2
, to find the closest match. The immediate solution that comes to mind is to use two loops (outer for everything in $arr1
and inner for each in $arr2
), but is there a function in PHP that can take a string and see if it matches any string in an existing array? Does anybody know of a more performant way to do this?