2011-01-10 133 views
-3

可能重複:
Outputting array contents as nested list in PHP顯示PHP中的數組內容/ HTML

I have the array `array ([0] => array(1,2,3,4,5) [1] => array(6,7,8,9,10))` and I would like to display it like this: 

<ul> 
    <li> 
    <a href=""/>FIRST ELEMENT OF THE array ==> 1</a> 
    <a href=""/>2ND ELEMENT OF THE TAB ==> 2</a> 
    <a href=""/>3THIRD ELEMENT==> 3</a> 
    <a href=""/>FORTH ELEMENT OF THE TAB ==> 4</a> 
    <a href=""/>FIFTH ELEMENT==> 5</a> 
</li> 
<li> 
    <a href=""/>6th ELEMENT==> 6</a> 
    <a href=""/>7th ELEMENT OF THE TAB ==> 7</a> 
    <a href=""/>8th ELEMENT==> 8</a> 
    <a href=""/>9th ELEMENT OF THE TAB ==> 9</a> 
    <a href=""/>10th ELEMENT OF THE TAB ==> 9</a> 
</li> 


</ul> 

我如何在PHP中實現這一目標?我正在考慮創建一個array_slice的子陣列。

+5

似乎與您以前的問題100%一樣:http://stackoverflow.com/questions/4647715/outputting-array-contents-as-nested-list-in-php – 2011-01-10 16:15:56

+0

-1:什麼費利克斯克林說過。 – 2011-01-10 16:19:28

+2

完成我以前的評論:如果迄今爲止的答案沒有幫助你,你應該回答你有什麼問題和/或澄清你原來的問題。轉發問題不會改善它。 – 2011-01-10 16:22:12

回答

0
<?php 

$myArray = array ("0" => array(1,2,3,4,5), "1" => array(6,7,8,9,10)); 

//open the list 
echo "<ul>"; 

foreach($myArray as $li){ 
    echo "<li>"; 
     foreach($li as $a){ 
      echo "<a href = ''>".$a."</a>"; 
     } 
    echo "</li>"; 
}  
//close the list 
echo "</ul>"; 

?> 
1
echo "<ul>"; 
foreach($yourArray as $subArray){ 
    echo "<li>"; 
    foreach($subArray as $value){ 
     echo "<a href=\"\">".$value."</a>"; 
    } 
    echo "</li>"; 
} 
echo "</ul>"; 
0
$my_array = array(array(1,2,3,4,5), array(6,7,8,9,10)); 

echo "<ul>\n"; 
foreach($my_array as $sub_array) { 
    echo "\t<li>\n"; 
    foreach($sub_array as $value) { 
    echo "\t\t<a href=\"\">".$value."</a>\n"; 
    } 
    echo "\t</li>\n"; 
} 
echo "</ul>\n"; 
0

試試這個:

$arr = array (
0 => array(1,2,3,4,5), 
1 => array(6,7,8,9,10), 
); 

echo '<ul>'; 
foreach($arr as $value) 
{ 
    if(is_array($value) && count($value > 0)) 
    { 
    echo '<li>'; 
    for($i =0; $i<count($value); $i++) 
    { 
     echo '<a href="">'.$value[$i].'</a>'; 
    } 
    echo '</li>'; 
    } 
} 
echo '</ul>';