显示ul li标签内的数组

时间:2013-06-03 21:34:30

标签: php arrays foreach

我有一个看起来像这样的数组,我希望以更易读的格式显示它。我想要鸟名(Gray Hawk等),然后是每个列表(结果的数量会有所不同。这是我目前的代码:

$xml = simplexml_load_file($noteable);
$result = array();
foreach ($xml->result->sighting as $sighting) {
    $location = (string) $sighting->{'loc-name'};
    $bird = (string) $sighting->{'com-name'};
    $howMany = (string) $sighting->{'how-many'};
    $obsdt = (string) $sighting->{'obs-dt'};
           $thenotedate = $obsdt;
       $thenotedate = split('T',$thenotedate);
       $thenotedate = $thenotedate[0];
       $thenotedate = strftime('%a %b %e at %I:%M %p',strtotime($thenotedate)); 

ksort($result);

   if (!isset($result[$bird])) $result[$bird] = array();
    $result[$bird][] = $howMany . ' seen at ' . $location . ' on ' . $thenotedate;
}
print"<pre>";   
print_r($result);
print"</pre>";
 }

这是数组

[Gray Hawk] => Array
    (
        [0] => 1 seen at Florida Canyon--lower on Sun Jun  2 at 04:50 PM
        [1] => 1 seen at Madera Canyon--Whitehouse Picnic area on Sat Jun  1 at 07:30 AM
        [2] => 1 seen at Florida Canyon--lower on Thu May 30 at 07:56 AM
        [3] => 1 seen at Florida Canyon--lower on Wed May 29 at 07:40 AM
        [4] => 1 seen at Florida Canyon--lower on Wed May 29 at 07:37 AM
        [5] => 1 seen at Madera Canyon--Madera Picnic Area on Tue May 28 at 04:45 PM
        [6] => 1 seen at Madera Canyon--Proctor Rd. on Mon May 27 at 09:40 AM
    )

[MacGillivray's Warbler] => Array
    (
        [0] => 1 seen at Madera Canyon--Proctor Rd. on Sat May 25 at 05:45 PM
        [1] => 1 seen at Madera Canyon--Proctor Rd. on Sat May 25 at 05:45 PM
        [2] => 1 seen at Madera Canyon--Proctor Rd. on Sat May 25 at 05:30 PM
    )

2 个答案:

答案 0 :(得分:1)

打印出结果时不要使用print_r($result);使用循环返回数组中的每个元素!如果你需要关于如何显示数组的信息,你想告诉我们你想要如何输出它

答案 1 :(得分:1)

尝试这样的事情 -

foreach ($result as $key=>$value){

    //echo Bird Name
    echo "<h3>$key</h3>";

    //start an unordered list
    echo "<ul>";    

    //echo each sighting     
    foreach($value as $row){
        echo "<li>$row</li>";
    }

    //close the unordered list
    echo "</ul>";
}