【问题标题】:Foreach returns a column and I have to turn it into a tableForeach 返回一列,我必须把它变成一个表
【发布时间】:2018-04-23 15:36:51
【问题描述】:

我有以下 JSON 表

[
    ["TITLE", "CONTENT", "DATE"],
    ["Monday", "Content of the monday post goes here", "09:55\n10:00\n10:30\n11:00\n15:45"],
    ["Tuesday", "Content of the tuesday day goes here", "08:00\n11:00\n16:00\n16:00\n21:00\n"],
]

我使用foreach 来获取json 的内容

$days = json_decode(file_get_contents('json_file'));
        foreach($days as $item){
            if(isset($item)){
                $firstColumn = $item;
            }
            echo $firstColumn[0];

        };

$firstColumn[0] 返回“标题”、“星期一”、“星期五”

如何将其添加到表格中?

<table class="table">
   <thead>
     <tr>
      <th> Title </th>
      <th>Content</th>
      <th>Date</th>
     </tr>
    </thead>
    <tbody>
      <tr>
       <td></td>
       <td></td>
       <td></td>
      </tr>
     </tbody>
</table>

【问题讨论】:

    标签: php json html-table


    【解决方案1】:

    如下所示:-

    <?php
    
    $days = [
        ["TITLE", "CONTENT", "DATE"],
        ["Monday", "Content of the monday post goes here", "09:55\n10:00\n10:30\n11:00\n15:45"],
        ["Tuesday", "Content of the tuesday day goes here", "08:00\n11:00\n16:00\n16:00\n21:00\n"],
    ]; // as you said that you got this array already
    unset($days[0]);
    ?>
    <table class="table">
       <thead>
         <tr>
          <th> Title </th>
          <th>Content</th>
          <th>Date</th>
         </tr>
        </thead>
        <tbody>
         <?php foreach($days as $day){?>
          <tr>
           <td><?php echo $day[0];?></td>
           <td><?php echo $day[1];?></td>
           <td><?php echo $day[2];?></td>
          </tr>
         <?php }?>
         </tbody>
    </table>
    

    在我的本地端输出:- https://prnt.sc/h8nit6

    关于您的问题:- but how can I add it into a return inside a function?。我想你想要如下:-

    function returnFullHtml(){
        $days = json_decode(file_get_contents('json_file'));// now you got the array what you shhown in your question input
        unset($days[0]);
        $html = '<table class="table">
            <thead>
                <tr>
                    <th> Title </th>
                    <th>Content</th>
                    <th>Date</th>
                </tr>
            </thead>
            <tbody>';
        foreach($days as $day){
            $html .= '<tr><td>'. $day[0] .'</td><td>' . $day[1] .'</td><td>' . $day[2] .'</td></tr>';
        }
        $html .= '</tbody></table>';
        return $html;
    
    }
    

    【讨论】:

    • 这可行,但我怎样才能将它添加到函数内的返回中?
    • @Radu033 你能详细说明一下吗:- but how can I add it into a return inside a function?
    • @Radu033 请检查我编辑的答案第二部分。是你想要的吗?
    【解决方案2】:

    你可以这样试试

    echo '<table class="table">';
        $items = json_decode(file_get_contents('json_file'));
        $headers = array_shift($items);
    
        echo '<thead>';
        foreach($headers as $value){
            echo '<th>'.$value.'</th>';
        }
        echo '</thead>';
    
        echo '<tbody>';
        foreach($items as $item){
            echo '<tr>';
            foreach($item as $value){
                echo '<td>'.$value.'</td>';
            }
            echo '</tr>';
        };
        echo '</tbody>';
    echo '</table>';
    

    【讨论】:

      【解决方案3】:

      使用下面的从任何给定的 json obj 渲染表格

      <?php
      if (sizeof($days) > 0) { //If json is not empty
          $firstColumn = $days[0] ; //First Column (<thead> values)
          ?>
          <table>
              <thead>
                  <tr>
                      <?php foreach ($firstColumn AS $k => $v) { ?>
                          <th><?php echo $v?></th>
                      <?php } ?>
                  </tr>
              </thead>
              <tbody>
                  <?php
                  foreach($days as $k => $v) {
                      if ($k != 0) { ?>
                          <tr>
                          <?php
                          foreach ($v AS $k1 => $v1) { ?>
                              <td><?php echo $v1 ?></td>
                          <?php
                          } ?>
                          </tr>
                      <?php
                      }
                  } ?>
              </tbody>
          </table>
          <?php
      } ?>
      

      【讨论】:

        猜你喜欢
        • 2023-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多