我正在使用的是一个区域鸟类的目击列表。有时同一只鸟被报告两次或三次。我想用它的名字对所有鸟类的所有目击进行分组,然后显示目击地点的位置名称。
到目前为止,这是我正在使用的,并且没有输出...在8小时......时间去寻求帮助。
以下是$ noteable的示例网址
<?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
}
}
?>
答案 0 :(得分: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]);