以PHP / HTML显示数组内容

时间:2011-01-10 16:12:36

标签: php arrays

  

可能重复:
  Outputting array contents as nested list in PHP

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 个答案:

答案 0 :(得分:1)

echo "<ul>";
foreach($yourArray as $subArray){
   echo "<li>";
   foreach($subArray as $value){
      echo "<a href=\"\">".$value."</a>";
   }
   echo "</li>";
}
echo "</ul>";

答案 1 :(得分:0)

我建议使用嵌套的foreach或语句:

http://php.net/manual/en/control-structures.foreach.php

即:

foreach(element in array){
//output <li>
foreach(element in array item array){
//output a tag
}
//output</li>
}

答案 2 :(得分: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>";

?>

答案 3 :(得分: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";

答案 4 :(得分: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>';