I can do this, I’m just wondering if there is a more elegant solution than the 47 hacked lines of code I came up with…
Essentially I have an array (the value is the occurrences of said string);
[Bob] => 2
[Theresa] => 3
[The farm house] => 2
[Bob at the farm house] => 1
I’d like to iterate through the array and eliminate any entries that are sub-strings of others so that the end result would be;
[Theresa] => 3
[Bob at the farm house] => 1
Initially I was looping like (calling this array $baseTags):
foreach($baseTags as $key=>$count){
foreach($baseTags as $k=>$c){
if(stripos($k,$key)){
unset($baseTags[$key]);
}
}
}
I’m assuming I’m looping through each key in the array and if there is the occurrence of that key inside another key to unset it… doesn’t seem to be working for me though. Am I missing something obvious?
Thank you in advance.
-H
You’re mis-using strpos/stripos. They can return a perfectly valid
0if the string you’re searching for happens to be at the START of the ‘haystack’ string, e.g. yourBobvalue. You need to explicitly test for this withif strpos/stripos don’t find the needle, they return a boolean false, which under PHP’s normal weak comparison rules is equal/equivalent to 0. Using the strict comparison operators (
===,!==), which compare type AND value, you’ll get the proper results.