【发布时间】:2013-06-03 21:34:29
【问题描述】:
我有一个看起来像这样的数组,我想以更易读的格式显示它。我想要鸟的名字(灰鹰等),然后是每个列表(结果的数量会有所不同。这是我目前拥有的代码:
$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
)
【问题讨论】:
-
你有什么问题?看来您知道如何使用
foreach以及如何输出 html 标签。你还需要什么吗? -
每次我尝试 foreach $result 我只得到第一只鸟/目击..无法弄清楚如何将它们全部回显..对不起,我真的很陌生:(
-
哦,我明白了。 @anze 的回答是正确的;
print_r通常用于调试,而不是实际向用户显示数据。只需循环通过$results和另一个foreach。