在ul li标签内显示数组 [英] display Array within ul li tags

查看:249
本文介绍了在ul li标签内显示数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的数组,我想以更易读的格式显示它。我想鸟的名字(灰鹰等),然后每个列表(其中结果的数量会有所不同。这是我现在有的代码:

  $ xml = simplexml_load_file($ noteable); 
$ result = array();
foreach($ xml-> result->瞄准为$ 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:% ($结果));

ksort($结果);

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

这是数组

  [Grey Hawk] =>阵列

[0] => 1在佛罗里达峡谷看到 - 在下午4点50下午在孙骏2
[1] => 1在马德拉峡谷看到 - Whitehouse 6月1日星期六上午07:30 AM
[2] => 1在佛罗里达峡谷出现的野餐区 - 5月30日星期四上午07:56较低
[3] => 1在佛罗里达峡谷出现 - 下周五5月29日上午07:40
[4] => 1在佛罗里达峡谷看到 - 在5月29日星期三上午07:37下降
[5] => 1在5月28日星期二下午04:45在马德拉峡谷 - 马德拉野餐区见过
[6] => 1在Madera Canyon - Proctor Rd。 AM


[MacGillivray's Warbler] => 5月25日星期五下午05:45在Madera Canyon - Proctor Rd。上看到阵列

[0] => 1
[1] => 1在Madera Canyon - 在5月25日星期五下午05:45的施工者路上
[2] => 1在Madera Canyon - Proctor Rd。5月25日(星期六)05:30 PM


解决方案


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

// echo鸟名
echo< ; H3> $密钥LT; / H3>中;

//开始一个无序列表
echo< ul>;

//回显每个发现
foreach($ value as $ row){
echo< li> $ row< / li>;
}

//关闭无序列表
echo< / ul>;
}


I have an array that looks like this and I want to display it in a more readable format. I would like the bird name (Gray Hawk etc) and then each listing (which the number of result will vary. This is the code I currently have:

$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>";
 }

And this is the array

[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
    )

解决方案

try something like this -

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>";
}

这篇关于在ul li标签内显示数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆