0

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.

Walt
  • 11
  • 2
  • I deleted my answer because I didn't fully understand what you were going for. Can you add var_dump("(".$gloc."|".$glocs[$g]."|".stripos($gloc, $glocs[$g]).")"); above the if statement? and show us the results – Chris Trudeau Dec 05 '15 at 21:04
  • Have you seen my answer? Please leave a comment. – moskito-x Dec 10 '15 at 13:53
  • Do you know how stackoverflow works ? – moskito-x Dec 19 '15 at 13:42
  • I know how it works. Thank you for your answer, I didn't have an opportunity to test it because I eventually decided I didn't need this feature at that point. When I return to it, I'll try to implement it first thing. – Walt Jul 08 '16 at 18:25

1 Answers1

0

A small logic error in your approach. You will overwrite only a similar value with the next.
Ok now you have two exact the same "Herzegovina".

$glocs =array("Federation of Bosnia and Herzegovina","Bosnia and Herzegovina",
              "Herzegovina","Germania");
...
$glocs[$g-1] = $glocs[$g];
...

Output:

array(4) {
  [0] =>
  string(22) "Bosnia and Herzegovina"
  [1] =>
  string(11) "Herzegovina"
  [2] =>
  string(11) "Herzegovina"
  [3] =>
  string(8) "Germania"
}

Look at the next solutions (deleted the double value).
Decide which approach is the better but must also improved.
Deletes the value "Herzegovina"
This may of course not be the final solution.
It's not that easy. For a comparison like that there are other considerations important.


<?php
$glocs =array("Federation of Bosnia and Herzegovina","Bosnia and Herzegovina",
              "Herzegovina","Germania");
$g = 1;
foreach ($glocs as $gloc) {
  // Just to see how it works
  echo '<pre>$gloc '.$gloc.' vs $glocs['.$g.'] '.$glocs[$g];
  if (stripos($gloc, $glocs[$g]) !==false OR stripos($glocs[$g], $gloc ) !== false ) {
      unset($glocs[$g]);
     }
  echo '</pre>';
  $g++;
}
 var_dump($glocs);  
?>  

Output:


array(2) {
  [0] =>
  string(36) "Federation of Bosnia and Herzegovina"
  [3] =>
  string(8) "Germania"
}
moskito-x
  • 11,832
  • 5
  • 47
  • 60