如何显示此多维数组

时间:2014-06-20 11:55:15

标签: arrays multidimensional-array

我创建了一个名为' orders'的多维数组。但我不知道如何以可读的方式向访问者显示它。如何在桌子或其他东西中显示它?

我想如何展示它:

<table>
  <tr>
    <td>Cake</td>
    <td>ingredient1</td>
    <td>ingredient2</td>
    <td>ingredient3</td>
    <td>ingredient</td>
  </tr>
  <tr>
    <td>Chocolate Cookie</td>
    <td>5</td>
  </tr>
  <!-- And go on... -->
 </table>

这是数组:

array(5) {
 [0]=>
   array(2) {
      [0]=> string(4) "Cake"
      [1]=> array(5) {
          [0]=> string(11) "ingredient1"
          [1]=> string(11) "ingredient2"
          [2]=> string(11) "ingredient3"
          [3]=> string(11) "ingredient4"
          [4]=> string(11) "ingredient5"
          }
    }
 [1]=> array(2) {
     [0]=> string(16) "Chocolate Cookie"
     [1]=> string(1) "5"
     }
 [2]=> array(2) {
     [0]=> string(15) "Chocolate cakes"
     [1]=> string() "10"
     }
 [3]=>
   array(2) {
      [0]=> string(6) "Cookie"
      [1]=> array(3) {
          [0]=> string(11) "ingredient1"
          [1]=> string(11) "ingredient2"
          [2]=> string(11) "ingredient3"
          }
    }

}

2 个答案:

答案 0 :(得分:1)

您可以通过以下方法在php中执行此操作

 $array=array();
  $array[0]=array(0 => 'cake',1 =>array(0 => 'ingredient1',1 => 'ingredient1',2 => 'ingredient1'));
  $array[1]=array(0 => 'pizza',1 =>array(0 => 'ingredient11',1 => 'ingredient12',2 => 'ingredient13'));

$html='<table>';
foreach($array as $arr){
$newarray=""; //Intialization
$html.='<tr>';
$html.='<td>'.$arr[0].'</td>';
$newarrays=$arr[1];
foreach($newarrays as $newarr){
$html.='<td>'.$newarr.'</td>';
}
$html.='</tr>';
}
$html.="</table>";
echo $html;

如果你想以同样的方式检查并告诉我

由于

答案 1 :(得分:0)

以下结构可能会有所帮助:

由于Cake下有另一个数组,需要在tr中显示一个列表,所以我建议使用嵌套表。

<table>
    <tr>
        <td>
            Cake
        </td>
        <td>
            <table>
                <tr>
                    <td>
                        indegredent1
                    </td>
                </tr>
                <tr>
                    <td>
                        indegredent2
                    </td>
                </tr>
                <tr>
                    <td>
                        indegredent3
                    </td>
                </tr>
                    <td>
                        indegredent4
                    </td>
                <tr>
                    <td>
                        indegredent5
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td>
            Chocolate cake
        </td>
        <td>
            5
        </td>
    </tr>
    and so on...
</table>