I'm trying to make a HTML table out of a multidimensional array variable $array2
which is a query(from a mysql database PHP query and Not a self-made query) and is as follows;
Array
(
[0] => Array
(
[0] => M2TYEE
[1] => Jean
[2] => Harvey
[3] => London
[4] => 0314686334
)
[1] => Array
(
[0] => E26YBE
[1] => Tom
[2] => Cruise
[3] => New York
[4] => 0635625735
)
)
I want to make the table appear like this
|ID |FIRST NAME| LAST NAME | CITY | PHONE |
________________________________________________________
|M2TYEE | Jean | Harvey | London | 0314686334 |
--------------------------------------------------
|E26YBE | Tom | Cruise | New York | 0635625735 |
They array may increase to upto say 1000 values so using a while/for loop would be necessary. Any help will be highly appreciated
You can use array_merge
and foreach
for the desired output
$arr = array_merge([ 0 => ['ID','FIRST NAME','LAST NAME','CITY','PHONE']],$arr);
$html = '<table border="1">';
foreach($arr as $row){
$html .= '<tr>';
foreach($row as $column){
$html .= '<td>'.$column.'</td>';
}
$html .= '</tr>';
}
$html .= '</table>';
echo $html;
because your internal array are fixed with size ( all have 4 field) , you can use single loop as below:
echo "<table>";
foreach($array2 as $index){
echo "<tr>";
echo "<td>".$index[0]."</td><td>".$index[1]."</td><td>".$index[2]."</td><td>".$index[3]."</td><td>".$index[4]."</td>";
echo "</tr>";
}
echo "</table>";
Another approach:
$result.="<table>";
foreach($array2 as $index){
$result.="<tr>";
$result.="<td>".$index[0]."</td><td>".$index[1]."</td><td>".$index[2]."</td><td>".$index[3]."</td><td>".$index[4]."</td>";
$result.="</tr>";
}
$result.="</table>";
echo $resul;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.