我有两个名字的数组
$names = array(1A,1B,1C,2A,2B,2C,3A,3B,3C);
$user_names = array(Jen, Smith, Nick, Rose, Jason, Ralph, Bruce, Linda, Kate);
<table class="stak">
<?php
$i = 1;
foreach ($names as $name) {
?>
<tr>
<td> <?php echo $i ?> </td>
<td> <?php echo $name ?> </td>
<td> <?php echo $user_name ?> </td>
<?php
$i++;?>
</tr><?php
}
$j = 1;
foreach ($user_names as $user_name) {
?>
<tr>
<td> <?php echo $j ?> </td>
<td> <?php echo $user_name ?> </td>
<?php
$j++;?>
</tr><?php
}
?>
</table>
我想在一个表中显示它们,其中包含3列Sl NO,名称,PHP中的用户名 请帮忙。
答案 0 :(得分:5)
解决方案:
$names = array("1A","1B","1C","2A","2B","2C","3A","3B","3C");
$user_names = array("Jen", "Smith", "Nick", "Rose", "Jason", "Ralph", "Bruce","Linda", "Kate");
$combine = array_combine($names, $user_names);
$html = "<table>";
$html .= "<tr><td>Sl.No</td><td>Name</td><td>Username</td></tr>";
$i = 1;
foreach ($combine as $names => $user_names):
$html .= "<tr>";
$html .= "<td>".$i."</td>";
$html .= "<td>".$names."</td>";
$html .= "<td>".$user_names."</td>";
$html .= "</tr>";
$i++;
endforeach;
$html .= "</table>";
echo $html;
输出
Sl.No Name Username
1 1A Jen
2 1B Smith
3 1C Nick
4 2A Rose
5 2B Jason
6 2C Ralph
7 3A Bruce
8 3B Linda
9 3C Kate
第二种情况(如果有超过2个数组):
$names = array("1A","1B","1C","2A","2B","2C","3A","3B","3C");
$user_names = array("Jen", "Smith", "Nick", "Rose", "Jason", "Ralph", "Bruce","Linda", "Kate");
$dob = array("12","13","14","15","16","17","18","19","20");
$height = array("6","7","8","5","4","7","5","9","5");
$html = "<table>";
$html .= "<tr><td>Sl.No</td><td>Name</td><td>Username</td><td>dob</td><td>height</td></tr>";
foreach ($names as $id => $key):
$html .= "<tr>";
$html .= "<td>".($id+1)."</td>";
$html .= "<td>".$key."</td>";
$html .= "<td>".$user_names[$id]."</td>";
$html .= "<td>".$dob[$id]."</td>";
$html .= "<td>".$height[$id]."</td>";
$html .= "</tr>";
endforeach;
$html .= "</table>";
echo $html;
答案 1 :(得分:1)
试试这个。有用。我测试了。
$names = array('1A','1B','1C','2A','2B','2C','3A','3B','3C');
$user_names = array('Jen', 'Smith', 'Nick', 'Rose', 'Jason', 'Ralph', 'Bruce', 'Linda', 'Kate');
$size = count($names);
$output = "<table>";
$output .= "<tr><td>Sl.No</td><td>Name</td><td>Username</td></tr>";
for($i=0; $i<$size; $i++) {
$output .= "<tr>";
$output .= "<td>".($i+1)."</td>";
$output .= "<td>".$names[$i]."</td>";
$output .= "<td>".$user_names[$i]."</td>";
$output .= "</tr>";
}
$output .= "</table>";
echo $output;
答案 2 :(得分:0)
这是一种在数组中显示数据的简单快捷的方法
<?php
// DATAS
$names = array('1A','1B','1C','2A','2B','2C','3A','3B','3C');
$user_names = array('Jen','Smith','Nick','Rose','Jason','Ralph','Bruce','Linda','Kate');
// HTML
$table = '<table><thead><tr>';
$table .= '<th>Name</th><th>User name</th>';
$table .= '</tr></thead><tbody>';
for ($i=0; $i < count($names) ; $i++) {
$table .= '<tr>';
$table .= '<td>'.$names[$i].'</td>';
$table .= '<td>'.$user_names[$i].'</td>';
$table .= '</tr>';
}
$table .= '</tbody>';
echo $table;
这是html结果
<table>
<thead>
<tr>
<th>Name</th>
<th>User name</th>
</tr>
</thead>
<tbody>
<tr>
<td>1A</td>
<td>Jen</td>
</tr>
<tr>
<td>1B</td>
<td>Smith</td>
</tr>
...
</tbody>