dongqing220586 2012-05-05 20:36
浏览 49

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)

  • 写回答

2条回答 默认 最新

  • dongqi0644 2012-05-05 20:45
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助