Select data from csv and make associative array
Well, first of all this data must be stored in a database, not a file. Using PDO you'll get your array in a few lines (assuming a database connection is already established):

$sql = "SELECT city, district FROM zip WHERE zipcode=? ORDER BY city, district";
$stmt = $pdo-&gt;prepare($sql);
$stmt-&gt;execute([$zipcode]);
$data = $stmt-&gt;fetchAll(PDO::FETCH_GROUP|PDO::FETCH_COLUMN); 


As of the code present, there are way too much loops to my taste. I believe everything could be done in one loop, like

$zipcode = '67401';
$data = [];
foreach (file("zip.csv", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $row) {
    list($zip, $city, $district) = explode(",",$row);
    if ($zip == $zipcode) {
        if (!isset($data[$city])) {
            $data[$city] = [];
        }
        $data[$city][] = $district;
    }
}


well if you need to sort your arrays, a couple extra loops are still needed

ksort($data);
foreach ($data as $city =&gt; $array) {
    sort($data[$city]);
}