Foreach返回一列,我必须把它变成一个表

时间:2017-11-10 11:54:55

标签: php json html-table

我有以下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>

3 个答案:

答案 0 :(得分:2)

使用以下内容从任何给定的json obj

渲染表
select concat(substring_index(col, ' : ', 1), ' : ',
              substring_index(substring_index(col, ' : ', 2), ' : ', -1)
             ) as status,
       concat(substring_index(substring_index(col, ' : ', 3), ' : ', -1),
              ' : ',
              substring_index(substring_index(col, ' : ', 4), ' : ', -1)
             ) as cause,
       concat(substring_index(substring_index(col, ' : ', 5), ' : ', -1),
              ' : ',
              substring_index(col, ' : ', -1)
             ) as comments

答案 1 :(得分:0)

如下所示: -

3.10

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

关于您的问题: - <?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> 。我想你想要如下: -

but how can I add it into a return inside a function?

答案 2 :(得分:0)

你可以这样试试

<input type="text" id="input1" />
<input type="button" value="Add More" id="btn1" />

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>