2013-06-03 70 views
0

我有一個看起來像這樣的數組,我想以更可讀的格式顯示它。我想鳥名(灰鷹等),然後每個上市(其結果的數量會有所不同這是我目前擁有的代碼:顯示陣列內UL LI標記

$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 
    ) 
+1

你的問題是什麼?它看起來像你知道如何使用'foreach'和如何輸出html標籤。你還需要什麼別的嗎? – sgroves

+0

每當我嘗試foreach $結果我只得到第一隻鳥/瞄準..不知道如何回聲他們all..sorry我其實真的是新的在此:( –

+2

ohhhh我gotcha。@ anze的回答是正確的;'print_r'通常用於調試,而不是實際向用戶顯示數據,只是通過'$ results'與另一個'foreach'循環。 – sgroves

回答

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

作品像一個寶石,非常感謝你!我想我從這裏得到它。 –

1

打印結果時不要使用print_r($result);使用一個循環返回數組中的每個元素!如果您需要關於如何顯示數組的信息,因爲您要準確告訴我們您希望如何輸出數據

+0

我希望結果回顯出來,就像它們顯示的名稱一樣作爲一個h3的鳥,然後在下面嵌套的目擊...感謝 –