-2

我在 php 中有这个数组:

$a[]="Jason White";
$a[]="Jean Black";
$a[]="Billy Brown";

但我必须在表中显示值,应该是这样的:

+-----------+--------------+
| Firstname | Lastname     |
+-----------+--------------+
| Jason     | White        |
+-----------+--------------+
| Jean      | Black        |
+-----------+--------------+
| Billy     | Brown        |
+-----------+--------------+

可以这样做吗?或者有什么建议可以制作这张桌子?

提前致谢。

4

4 回答 4

1

试试喜欢

echo "<table>";
foreach($a as $name) {
   echo "<tr>";
   $name_arr = explode(" ",$name);
   echo "<td>".$name_arr[0]."</td>";
   echo "<td>".$name_arr[1]."</td>";
   echo "</tr>";
}
echo "</table>";
于 2013-08-22T06:23:28.213 回答
0

对的,这是可能的。此示例假定不必从 PHP 生成文本“Firstname”和“Lastname”:

<?php
$a = array
(
  'Jason White',
  'Jean Black',
  'Billy Brown',
);
?>

<table>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
  </tr>
<?php
  foreach($a as $val)
  {
    // creates an array from a string 
    // basing on a string character 
    // to be used as an indicator of new array value
    $separate = explode(' ', $val);

    echo '<tr>';

    // to avoid 'undefined index' errors, 
    // loop through the array that was just created.
    foreach($separate as $display)
    {
      echo '<td>'. $display .'</td>';
    }

    echo '</tr>';
  }
?>

</table>
于 2013-08-22T06:59:41.613 回答
0

要将字符串拆分并存储到数组中,请使用 PHP 函数explode

list($firstName, $lastName) = explode(" ", $var);
于 2013-08-22T07:03:50.657 回答
0

用空间炸开它并展示它

于 2013-08-22T06:23:49.573 回答