PHP:如何在表上打印每个数组值

时间:2014-11-18 10:15:34

标签: php arrays

所以我尝试从用户表中检索所有数据并将其放在数组上。

这是数组输出:

Array ( [error] => false 
  [users] => Array ( 
    [0] => Array ( 
      [user_id] => 34
      [name] => test1
      [status] => actived ) 
    [1] => Array ( 
      [user_id] => 35
      [name] => test2
      [status] => actived ) 
  )
)

这是我在HTML中的表格代码:

<table class="table table-bordered table-striped table-condensed">
  <thead>
    <tr>
      <th>Code</th>
      <th>Company</th>
      <th class="numeric">ID</th>
      <th class="numeric">Name</th>
      <th class="numeric">Status</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="numeric"><?php ID VALUE HERE ?></td>
      <td class="numeric"><?php NAME VALUE HERE ?></td>
      <td class="numeric"><?php STATUS VALUE HERE ?></td>
    </tr>
  </tbody>
</table>

我已经尝试过这样的预告:

foreach ($error as $key => $val) {

}

并将print_r ($val[0]['name']);放入NAME VALUE。 但它只是让桌子最糟糕。

如何将每个数组值放入该表中?

最好的方法是什么? 谢谢:))

6 个答案:

答案 0 :(得分:1)

foreach()

试试这个:假设变量名变量

<tbody>
    <tr>
<?php
foreach($variable['users'] as $user)
{ ?>
  <td class="numeric"><?php echo $user['user_id'] ?></td>
      <td class="numeric"><?php echo $user['name'] ?></td>
      <td class="numeric"><?php echo $user['status'] ?></td
<?php }?>
</tr>
  </tbody>

答案 1 :(得分:1)

你可以像这样使用

 $arr = Array ( [error] => false 
                [users] => Array ( 
                      [0] => Array ( 
                                    [user_id] => 34
                                    [name] => test1
                                    [status] => actived ) 
                      [1] => Array ( 
                                    [user_id] => 35
                                    [name] => test2
                                    [status] => actived ) 
                    ) 
              );

 <table class="table table-bordered table-striped table-condensed">
     <thead>
       <tr>
           <th>Code</th>
           <th>Company</th>
           <th class="numeric">ID</th>
           <th class="numeric">Name</th>
           <th class="numeric">Status</th>
        </tr>
       </thead>
       <tbody>
        <?php foreach($arr['users'] as $user): ?>
         <tr>
           <td class="numeric"><?php echo $user['user_id'] ?></td>
           <td class="numeric"><?php echo $user['name'] ?></td>
           <td class="numeric"><?php echo $user['status'] ?></td>
          </tr>
        <?php endforeach; ?>
        </tbody>
</table>

这将为您提供准确的输出。

答案 2 :(得分:1)

$data = Array ( [error] => false 
    [users] => Array ( 
                      [0] => Array ( 
                                    [user_id] => 34
                                    [name] => test1
                                    [status] => actived ) 
                      [1] => Array ( 
                                    [user_id] => 35
                                    [name] => test2
                                    [status] => actived ) 
                    ) );


<table class="table table-bordered table-striped table-condensed">
     <thead>
       <tr>
           <th>Code</th>
           <th>Company</th>
           <th class="numeric">ID</th>
           <th class="numeric">Name</th>
           <th class="numeric">Status</th>
        </tr>
       </thead>
       <tbody>
<?php foreach($data['users'] as $user):?>
         <tr>
           <td class="numeric"><?php echo $user['user_id']; ?></td>
           <td class="numeric"><?php echo $user['name']; ?></td>
           <td class="numeric"><?php echo $user['status']; ?></td>
          </tr>
<?php endforeach;?>
        </tbody>
</table>

答案 3 :(得分:1)

<table class="table table-bordered table-striped table-condensed">
  <thead>
    <tr>
      <th>Code</th>
      <th>Company</th>
      <th class="numeric">ID</th>
      <th class="numeric">Name</th>
      <th class="numeric">Status</th>
    </tr>
  </thead>
  <tbody>
    <?php 
    foreach($array['user'] as $user) {
        ?>
        <tr>
          <td class="numeric"><?php echo  $user['user_id'] ?></td>
          <td class="numeric"><?php echo  $user['name'] ?></td>
          <td class="numeric"><?php echo  $user['status'] ?></td>
        </tr>   
        <?php 
    }
    ?>
  </tbody>
</table>

答案 4 :(得分:0)

尝试以下代码:

<?php foreach($arrayData['users'] as $data){ ?>
     <td class="numeric"><?php echo $data['id']; ?></td>
      }

答案 5 :(得分:0)

<table class="table table-bordered table-striped table-condensed">
  <thead>
    <tr>
      <th>Code</th>
      <th>Company</th>
      <th class="numeric">ID</th>
      <th class="numeric">Name</th>
      <th class="numeric">Status</th>
    </tr>
  </thead>
  <tbody>
  <?php
  foreach($my_array['users'] as $value){
    echo '<tr>'.chr(13);
    echo '  <td class="numeric">'.$value['user_id'].'</td>'.chr(13);
    echo '  <td class="numeric">'.$value['name'].'</td>'.chr(13);
    echo '  <td class="numeric">'.$value['status'].'</td>'.chr(13);
    echo '</tr>'.chr(13);
  }
  ?>
  </tbody>
</table>