开发者

php create table from array

开发者 https://www.devze.com 2023-03-06 15:46 出处:网络
Array ( [0] => stdClass Object ( [logtime] => 1305732210 [useragent] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0
Array ( [0] => stdClass Object ( 
                  [logtime] => 1305732210 
                  [useragent] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0 
                  [remotehost] => 188-24-176-75.rdsnet.ro 
                  [page] => RSS.php 
                  [qs] => [action] => view page )

        [1] => stdClass Object ( 
                  [logtime] => 1305732216 
                  [useragent] => Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0 
                 开发者_JAVA技巧 [remotehost] => 188-24-176-75.rdsnet.ro 
                  [page] => Pages.php 
                  [qs] => page=angajari 
                  [action] => view page ) 
)

How to display this array as a table

             **| crt |logtime | useragent  | remotehost | action |
             ---------------------------------------------------
array values |  1  |        |            |            |        |
             ---------------------------------------------------
array values |  2  |        |            |            |        |
             ---------------------------------------------------**


echo '<table><tr><th>....';
foreach($arr as $o){
  echo >>>EOTR
<tr>
  <td>{$o->logtime}</td>
  <td>{$o->useragent}</td>
  <td>{$o->remotehost}</td>
  <td>{$o->page}</td>
  <td>{$o->action}</td>
</tr>
EOTR;
}
echo '</table>';


dnagirl's method works, but here's "template format", in case you need it that way (note: replace <?= with <?php echo if you don't have control of your server and/or PHP's short_tags is turned off):

<table><tr><th>
<?php foreach($arr as $o): ?>

<tr>
  <td><?= $o->logtime ?></td>
  <td><?= $o->useragent ?></td>
  <td><?= $o->remotehost ?></td>
  <td><?= $o->page ?></td>
  <td><?= $o->action ?></td>
</tr>
<?php endforeach; ?>
</table>


edit: sorry, i've totally missed that that's an object inside the array. this solution is wrong.

this would be the more general solution.

echo '<table><tr><td>crt</td>';
    foreach($arr[0] as $key => $val1){
      echo "<td>$key</td>";
    }
    echo '</tr>';
    foreach($arr as $key => $val){
      echo " <tr> <td>".($key+1)."</td>";
        foreach($val as $key => $val1){
          echo "<td>$val1</td>";
        }
      echo " </tr> ";
    }
    echo '</table>';
0

精彩评论

暂无评论...
验证码 换一张
取 消