Reputation: 539
I have two arrays by name
$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>
I want to display them in one table with 3 columns Sl NO, Names, User Names in PHP Please help.
Upvotes: 0
Views: 3883
Reputation: 4564
Solution :
$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;
Output :
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
Second case (In case of more then 2 arrays):
$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;
Upvotes: 5
Reputation: 576
Here is a simple and a fast way to display your datas in a array
<?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;
Here is the html result
<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>
Upvotes: 0
Reputation: 5108
Try this. It works. I tested.
$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;
Upvotes: 1