I have an array of Google location entities taken from Geocoding API ($glocs). Sometimes one element of the array partially repeats in another ("Federation of Bosnia and Herzegovina, Bosnia and Herzegovina", for instance). As I output them in the frontend by imploding the array separated by comma, I want the output to look less robotic. I wrote this code to try to avoid repeating names:
$g = 1;
foreach($glocs as $gloc) {
echo '<pre>$gloc ' . $gloc . ' vs $glocs[' . $g . '] ' . $glocs[$g]; // Just to see how it works
if (stripos($gloc, $glocs[$g]) !== false OR stripos($glocs[$g], $gloc) !== false) {
$glocs[$g - 1] = $glocs[$g];
}
echo '</pre>';
$g++;
}
It's supposed to check if every element of the array contains the next element, and vice versa. When found, it replaces current element with the next one (leaving "Bosnia and Herzegovina, Bosnia and Herzegovina"). Subsequent array_unique is supposed to finish the job.
The problem is the 'if' section doesn't work. If I replace 'false' with '0', it is triggered in all cases. I've also fiddled with this code in other ways (=== true instead of !== false or $gloc instead of $glocs[$g-1], for instance), but it didn't work the way I wanted.
Please, help me solve my problem. Maybe there is another approach to it that I'm missing. Thanks.
Eventually I created an array of redundant words like "Federation of", "Arrondisement" (for Paris) and started running the elements against this array, eliminating ones containing a redundant word.