php:使用google csv,删除列中填充的重复值

I am a newbie to php and have been searching tirelessly for a solution to this problem (i'll bet its a super simple solve too *sigh).

I am importing a .csv feed from a google doc. It is pulling in 2 columns, one for "name" and the other "location". I would like to remove duplicate "locations". since i am using fgetcsv, my understanding is that it is already sorting the data into an array. Ideally, it would omit the "location" duplicates so that the "names" look as though they are listed under the "location" they correspond to.

Here is what i have:

    $url = "https://docs.google.com/spreadsheet/pub?key=0AsMT_AMlRR9TdE44QmlGd1FwTmhRRkFHMzFTeTZhS3c&output=csv";
    $handle = fopen($url, "r");
    while (($data = fgetcsv($handle, 5000, ",")) !== FALSE) {
    echo "<li>
";
    echo $data[1];
    echo "<br/>
";
    echo $data[2];
    echo "</li>
";
    }
    fclose($handle);

ideally i would be able to use something like this:

    $url = "https://docs.google.com/spreadsheet/pub?key=0AsMT_AMlRR9TdE44QmlGd1FwTmhRRkFHMzFTeTZhS3c&output=csv";
    $handle = fopen($url, "r");
    while (($data = fgetcsv($handle, 5000, ",")) !== FALSE) {
    echo "<li>
";
    echo array_unique($data[1]);
    echo "<br/>
";
    echo $data[2];
    echo "</li>
";
    }
    fclose($handle);

Many thanks in advance for any help! :o)

This may work, assuming that the items in the array are grouped by location. It stores the last data item (location) and compares whether each item has that location. If it does, it prints it, otherwise it creates a new list item with the new location, and then prints the name underneath (I haven't tested it though):

$url = "the-url-to-my-csv-feed";
$handle = fopen($url, "r");
$lastdata = '';
while (($data = fgetcsv($handle, 5000, ",")) !== FALSE) {
    if ($lastdata == '') {
        echo "<li><strong>" . $data[1] . "</strong>
";
        echo "<br/>
";
        $lastdata = $data[1];
    }

    if ($lastdata != $data[1]) {
        echo "</li>
";
        echo "<li><strong>" . $data[1] . "</strong>
";
        echo "<br/>
";
        $lastdata == $data[1];
    }
    echo $data[2] . "<br/>
";
}
fclose($handle);
<? //PHP 5.4+
$url = 'url to your csv feed';

//Group people by same location first,
//not assuming csv is already sorted.
$namesByLocations = [];
//Because we're using \SplFileObject, when the reference goes out
//of scope at the end of the loop, the file pointer is never
//left open. This is true even if an exception is thrown
//in the middle of looping.
foreach(
    \call_user_function(static function() use ($url){
        $file = new \SplFileObject($url);
        $file->setFlags(\SplFileObject::READ_CSV);
        return $file;
    })
    as $array
){
    //$array[1] is assumed to be location string
    //$array[2] is assumed to be a name that is there.
    $namesByLocations[$array[1]][] = $array[2];
}


foreach($namesByLocations as $location => $names){
    //Protect against injection flaws,
    //escape to destination's context. (html this time)
    echo '<strong>' . \htmlspecialchars($location) . '</strong>';
    echo '<ul>';
    foreach($names as $name){
        echo '<li>' . \htmlspecialchars($name) . '</li>';
    }
    echo '</ul>';
}
?>