1

我正在处理的是一个地区鸟类的目击清单。有时同一只鸟会被报道两次或三次。我想按其名称对特定鸟类的所有目击事件进行分组,然后显示目击事件的位置名称。

到目前为止,这就是我正在使用的东西,并且没有输出……持续 8 小时……寻求帮助的时间。

这是 $noteable 的示例 url

http://ebird.org/ws1.1/data/notable/geo/recent?lng=-110.7576749&lat=32.4432180&detail=full&hotspot=true&dist=15&back=10

<?php
    $notexml = simplexml_load_file($noteable);
    $typesListXml = $notexml->xpath("result/sighting/com-name"); 

    if (!empty($typesListXml)) {
        $typesList = array();
        foreach ($typesListXml as $typeXml) {
            $typesList[] = (string)$typeXml;
        }
        $typesList = array_unique($typesList);

        $nameForType = array();
        foreach ($typesList as $type) {
            $rawData = $xml->xpath('result/sighting[com-name="' . $type . '"]');
            if (!empty($rawData)) {
                foreach ($rawData as $rawName) {
                    $nameForType[$type][] = $rawName->{'loc-name'};
                }
            }
        }

        var_dump($nameForType); // var_dump #4
    } 
}
?>
4

1 回答 1

1

这样的事情怎么样?

<?php
    $noteable = 'http://ebird.org/ws1.1/data/notable/geo/recent?lng=-110.7576749&lat=32.4432180&detail=full&hotspot=true&dist=15&back=10';
    $xml = simplexml_load_file($noteable);
    $result = array();
    foreach ($xml->result->sighting as $sighting) {
        $location = (string) $sighting->{'loc-name'};
        $bird = (string) $sighting->{'com-name'};
        if (!isset($result[$bird])) $result[$bird] = array();
        $result[$bird][] = $location;
    }
    print_r($result);

对于上面包含的 XML 文件,它会产生以下输出:

Array
(
    [Buff-breasted Flycatcher] => Array
        (
            [0] => Mt. Lemmon--Rose Canyon and Lake
            [1] => Mt. Lemmon--Rose Canyon and Lake
            [2] => Mt. Lemmon--Rose Canyon and Lake
        )

    [Northern Goshawk] => Array
        (
            [0] => Mt. Lemmon--Rose Canyon and Lake
        )

)

如果您想避免报告同一只鸟的重复位置,您可以array_unique在循环末尾添加一个调用:

$result[$bird] = array_unique($result[$bird]);
于 2013-06-03T18:52:26.647 回答