【问题标题】:Output php multidimensional array to html list将php多维数组输出到html列表
【发布时间】:2015-11-11 12:18:49
【问题描述】:

我在 php 脚本中设置了以下数组,我正试图按照下面的代码将其输出。我试过循环,但我不知道如何让它显示子数组。我希望有人可以帮助我。

PHP 数组

$fruits = array();
$fruits[] = array('type' => 'Banana', 'code' => 'ban00');
$fruits[] = array('type' => 'Grape', 'code' => 'grp01');
$fruits[] = array('type' => 'Apple', 'code' => 'apl00',
               array('subtype' => 'Green', 'code' => 'apl00gr'),
               array('subtype' => 'Red', 'code' => 'apl00r')
            );
$fruits[] = array('type' => 'Lemon', 'code' => 'lem00');

期望的输出

<ul>
    <li><input type="checkbox" name="fruit" value="Banana"> Banana</li>
    <li><input type="checkbox" name="fruit" value="Grape"> Grape</li>
    <li>Apple
        <ul>
            <li><input type="checkbox" name="fruit" value="Green"> Green</li>
            <li><input type="checkbox" name="fruit" value="Red"> Red</li>
        </ul>
    </li>
    <li><input type="checkbox" name="fruit" value="Lemon"> Lemon</li>
</ul>

【问题讨论】:

  • 您有尝试过的代码示例吗?你甚至可以输出一个单维数组吗?如果您这样做,我们可以更轻松地继续前进。

标签: php html multidimensional-array


【解决方案1】:

您可以使用递归函数。请注意,这是一个示例,而不是直接的解决方案,因为我们在这里学习而不是完成工作 - 根据需要进行更改。

function renderList(array $data) {
   $html = '<ul>';
   foreach ($data as $item) {
      $html .= '<li>';
      foreach ($item as $key => $value) {
         if (is_array($value)) {
             $html .= renderList($value);
         } else {
             $html .= $value;
         }
      }
      $html .= '</li>';
   }
   $html .= '</ul>';
   return $html;
}

$data = array();
$data[] = array('A');
$data[] = array('B', array(array('C')));
$data[] = array('D');

echo renderList($data);

这个输出将是:

  • 一个
  • B
    • C
  • D

或者html形式:

<ul>
    <li>A</li>
    <li>B
        <ul>
            <li>C</li>
        </ul>
    </li>
    <li>D</li>
</ul>

【讨论】:

    【解决方案2】:

    你需要做这样的事情。我没有尝试为你编码,只是想给你一个想法。我希望你能继续。

    echo '<ul>';
        foreach( $fruits as $fruit){
        echo  '<li>' . $fruit . '</li>';
            if( is_array($fruit) ){
                 echo  '<ul>';
                 foreach( $fruit as $fr ){
                     echo '<li>' . $fr . '</li>';
                 }
               echo '</ul>';
        }
            }echo '</ul>';
        }
    

    【讨论】:

      【解决方案3】:
      <?php
      
      $fruits = array();
      $fruits[] = array(
          'type' => 'Banana',
          'code' => 'ban00'
      );
      $fruits[] = array(
          'type' => 'Grape',
          'code' => 'grp01'
      );
      $fruits[] = array(
          'type' => 'Apple',
          'code' => 'apl00',
          array(
              'subtype' => 'Green',
              'code' => 'apl00gr'),
          array(
              'subtype' => 'Red',
              'code' => 'apl00r'
          )
      );
      $fruits[] = array(
          'type' => 'Lemon',
          'code' => 'lem00'
      );
      
      iterate($fruits, 'type');
      
      function iterate($fruits, $index)
      {
          foreach ($fruits as $fruit) {
              echo "<ul>";
              display($fruit, $index);
              echo "</ul>";
          }
      }
      
      function display($data, $index)
      {
          echo '<li><input type="checkbox" name="fruit" value="' . $data[$index] . '">' . $data[$index];
          if (!empty($data[0]) && is_array($data[0])) {
              $newSubArray = $data;
              unset($newSubArray['type']);
              unset($newSubArray['code']);
              iterate($newSubArray, 'subtype');
          }
          echo "</li>";
      }
      

      【讨论】:

        猜你喜欢
        • 2011-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 2015-01-05
        • 2021-03-21
        • 1970-01-01
        • 2020-05-27
        相关资源
        最近更新 更多