I’m trying to display a multidimensional array from my database into an Html table with PHP, row by row instead of column by column.
I have the following array;
JavaScript
x
Array (
[0] => Array (
[0] => Paracetamol
[1] => Caffeine
[2] => Pase
)
[1] => Array (
[0] => 12
[1] => 10
[2] => 1
)
[2] => Array (
[0] => Packets
[1] => Cartons
[2] => Containers
)
)
I tried using foreach and here is the output I got;
https://jsfiddle.net/fraqy912/1/
But, this is the output that I’m actually trying to achieve;
https://jsfiddle.net/5akgjtvf/3/
Advertisement
Answer
JavaScript
<?php
$array = Array (
Array (
'Paracetamol',
'Caffeine',
'Pase'
),
Array (
12,
10,
1
),
Array (
'Packets',
'Cartons',
'Containers'
)
);
function combine($array) {
array_unshift($array, null);
return call_user_func_array('array_map', $array);
}
$array = combine($array);
?>
<table border="1">
<tr>
<th>Product_name</th>
<th>Product_quantity</th>
<th>Product_size</th>
</tr>
<?php foreach($array as $row): ?>
<tr>
<td><?=$row[0]?></td>
<td><?=$row[2]?></td>
<td><?=$row[1]?></td>
</tr>
<?php endforeach ?>
</table>