1. <small id='HpTuc'></small><noframes id='HpTuc'>

      <i id='HpTuc'><tr id='HpTuc'><dt id='HpTuc'><q id='HpTuc'><span id='HpTuc'><b id='HpTuc'><form id='HpTuc'><ins id='HpTuc'></ins><ul id='HpTuc'></ul><sub id='HpTuc'></sub></form><legend id='HpTuc'></legend><bdo id='HpTuc'><pre id='HpTuc'><center id='HpTuc'></center></pre></bdo></b><th id='HpTuc'></th></span></q></dt></tr></i><div id='HpTuc'><tfoot id='HpTuc'></tfoot><dl id='HpTuc'><fieldset id='HpTuc'></fieldset></dl></div>
      • <bdo id='HpTuc'></bdo><ul id='HpTuc'></ul>

        <legend id='HpTuc'><style id='HpTuc'><dir id='HpTuc'><q id='HpTuc'></q></dir></style></legend>

        <tfoot id='HpTuc'></tfoot>
      1. PHP二维数组转HTML

        时间:2023-10-04

        1. <legend id='XPtMb'><style id='XPtMb'><dir id='XPtMb'><q id='XPtMb'></q></dir></style></legend>
              <tbody id='XPtMb'></tbody>
            <i id='XPtMb'><tr id='XPtMb'><dt id='XPtMb'><q id='XPtMb'><span id='XPtMb'><b id='XPtMb'><form id='XPtMb'><ins id='XPtMb'></ins><ul id='XPtMb'></ul><sub id='XPtMb'></sub></form><legend id='XPtMb'></legend><bdo id='XPtMb'><pre id='XPtMb'><center id='XPtMb'></center></pre></bdo></b><th id='XPtMb'></th></span></q></dt></tr></i><div id='XPtMb'><tfoot id='XPtMb'></tfoot><dl id='XPtMb'><fieldset id='XPtMb'></fieldset></dl></div>
              • <bdo id='XPtMb'></bdo><ul id='XPtMb'></ul>
              • <tfoot id='XPtMb'></tfoot>

                <small id='XPtMb'></small><noframes id='XPtMb'>

                  本文介绍了PHP二维数组转HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  一个简单的谷歌搜索将揭示多种使用 PHP 将二维数组转换为 HTML 的解决方案.不幸的是,这些都没有我正在寻找的答案.

                  A simple Google search will reveal a multitude of solutions for converting a two dimension array into HTML using PHP. Unfortunately none of these has the answers I am looking for.

                  我想要一段将数组转换为 HTML 表格的通用代码.大多数例子出错的地方是他们假设程序员知道表的字段的名称.我希望这段代码是通用的,这样即使我不知道字段的名称,我也可以使用它.

                  I want a generic piece of code that converts an array into a HTML table. Where most examples go wrong is that they assume the programmer knows the name of the table's fields . I want this code to be generic such that I can use it even if I do not know the name of the fields.

                  我可以看到我需要两个循环.一个嵌套在另一个里面.鉴于我不知道密钥,我不确定如何获取值.

                  I can see I need two loops. One nested inside of the other. What I am not sure of is how to get the values out given I don't know the keys.

                  最终结果有望输出这样的html:

                  The end result will hopefully output html something like this:

                  <th>
                    <td>x/y</td>
                    <td> x1 </td>
                    <td> x2 </td>
                  </th>
                  <tr>
                    <td>y1</td>
                    <td> x1y1 </td>
                    <td> x2y1 </td>
                  </tr>
                  <tr>
                    <td>y2</td>
                    <td> x1y2 </td>
                    <td> x2y2 </td>
                  </tr>
                  

                  请记住,我想要一个通用且简单的解决方案.我希望这很清楚.

                  Please remember I want a generic and simple solution. I hope this is clear.

                  推荐答案

                  下面的代码将遍历数组的两个维度,并将它们变成一个表格.不管 key 是什么,你都会得到它的可视化表示.如果它们确实有键名而不仅仅是索引,则这些值将分别在$key$subkey 中可用.所以如果你需要它们,你可以拥有它们.

                  The following code will look through the two dimensions of the array and make them into a table. Regardless of what the key may be, you will get a visual representation of it. If they do have key name and not just an index, the values will be available in $key and $subkey respectively. So you have them if you need them.

                  代码:

                  $myarray = array("key1"=>array(1,2,3,4),
                                   "key2"=>array(2,3,4,5),
                                   "key3"=>array(3,4,5,6),
                                   "key4"=>array(4,5,6,7)); //Having a key or not doesn't break it
                  $out  = "";
                  $out .= "<table>";
                  foreach($myarray as $key => $element){
                      $out .= "<tr>";
                      foreach($element as $subkey => $subelement){
                          $out .= "<td>$subelement</td>";
                      }
                      $out .= "</tr>";
                  }
                  $out .= "</table>";
                  
                  echo $out;
                  

                  结果:

                  如果您想将键视为标题,可以在 echo "

                  "; 行之后添加此代码:

                  If you want to see the keys as headings, you could add this code after the echo "<table>"; line:

                  echo "<tr>";
                  foreach($myarray as $key => $element) echo "<td>$key</td>";
                  echo "</tr>";
                  

                  导致:

                  这篇关于PHP二维数组转HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                    <tfoot id='O3UU2'></tfoot>

                    <small id='O3UU2'></small><noframes id='O3UU2'>

                      <tbody id='O3UU2'></tbody>

                          <bdo id='O3UU2'></bdo><ul id='O3UU2'></ul>
                          <legend id='O3UU2'><style id='O3UU2'><dir id='O3UU2'><q id='O3UU2'></q></dir></style></legend>

                          <i id='O3UU2'><tr id='O3UU2'><dt id='O3UU2'><q id='O3UU2'><span id='O3UU2'><b id='O3UU2'><form id='O3UU2'><ins id='O3UU2'></ins><ul id='O3UU2'></ul><sub id='O3UU2'></sub></form><legend id='O3UU2'></legend><bdo id='O3UU2'><pre id='O3UU2'><center id='O3UU2'></center></pre></bdo></b><th id='O3UU2'></th></span></q></dt></tr></i><div id='O3UU2'><tfoot id='O3UU2'></tfoot><dl id='O3UU2'><fieldset id='O3UU2'></fieldset></dl></div>