2013-06-03 4 views
1

내가 작업하고있는 것은 지역의 새들에 대한 목격 목록입니다. 때로는 같은 새가 두세 번보고됩니다. 특정 새의 모든 목격을 이름으로 그룹화 한 다음 목격을위한 위치 이름을 표시하고 싶습니다.PHP로 그룹화 된 XML 요소

이것은 내가 지금까지 작업하고 있으며 아무런 출력도 얻지 못하고 있습니다. 8 시간 후에 진행 중이며 도움을 요청합니다. 여기

어떻게 이런 일 약 $ 주목할만한

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 
    } 
} 
?> 

답변

1

의 샘플 URL입니다? 같은 조류에 대한보고 위치가 중복을 피하려면

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 호출을 추가 할 수 있습니다 위 포함 된 XML 파일의 경우

<?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); 

, 그것은 다음과 같은 출력을 생성 루프의 :

$result[$bird] = array_unique($result[$bird]); 
+0

이것은 완벽하게 너무 감사합니다! 그런 더 쉬운 해결책! –

+0

용서해주세요.하지만 배열로 일하는 것이 새로운 편입니다. ul li 구조를 사용하여 이것을 어떻게 반향시킬 수 있습니까? 미리 감사드립니다! –