Question

I am parsing a CSV file using fgetcsv, specifically using $line_of_text. I want to echo all the cities that have a shared country, but I want to eliminate city duplicates so that if, for example, Paris occurred 200 times it would only be echoed once, along a single echo for the other distinct cities of France regardless of their number of instances.

My hunch is that I need to store the city values in an array and then use array_unique to remove duplicates, but unfortunately this is beyond my current php abilities. Any help deeply appreciated, I've tried everything in my powers!

<?php
  $display = 100;
  $counter = 1;
  $country = $_GET['country'];
  echo "<ol>";
  $file_handle = fopen("csv/file.csv", "r");
  while (($line_of_text = fgetcsv($file_handle, 1024, ",")) !== false) {
      if ($line_of_text[13] == $country) {
          echo "<li>City:" . $line_of_text[15]) . "</li>";

          $counter++;
          if ($counter == $display) {
              break;
              echo "</ol>";
          }
      }
  }
  fclose($file_handle);
?>

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top