I do have a little problem in creating table from this dimentional array.
I am getting dynamic array from one page like,
Array
(
[0] => Array
(
[0] => 1374
[id] => 1374 )
[1] => 223
[version_id] => 223
[1] => Array
(
[0] => 1540
[id] => 1540)
[1] => 224
[version_id] => 224
[2] => Array
(
[0] => 1541
[id] => 1541)
[1] => 225
[version_id] => 225
)
I want data in table like
And what is the problem you’re having?
I have written code
$n = count($multidim_arr);
$z = count($multidim_arr[$n-1])/2;
$keys = array(‘ID’, ‘version_id’);
echo “<table>”;
for($j = 0; $j<$z;$j++) {
echo “<tr>”;
for($i = 0; $i<$n; $i++) {
echo “<td>”.$keys[$j]." and value: “.$multidim_arr[$i][$keys[$j]].”</td>";
}
echo “</tr>”;
}
echo “</table>”;
but its printing “<tr><td>ID and value: </td><td>ID and value: </td><td>ID and value: </td></tr>”.
But i want, <tr><td>ID </td><td>value1</td><td>value2</td><td>value3</td></tr>
I want to print the field_name one time in <td> and then the records, just like the comparison between 3 items.
tom8
4
Try the code below and see if this is what you are looking for:
<?php
$multidim_arr = array(array("First",1374,223),array("Second",1540,224),array("Third",1541,225));
echo "<pre>";print_R($multidim_arr);echo "</pre>";
$keys = array('Name', 'ID', 'version_id');
$rows = count($keys);
$cols = count($multidim_arr);
echo '<table border="1">';
for($j = 0; $j<$rows;$j++)
{
echo "<tr><td>$keys[$j]</td>";
for($i = 0; $i<$cols; $i++)
{
echo "<td>".$multidim_arr[$i][$j]."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
tom8
5
BTW, if you are getting the array as you posted then you need this code:
<?php
$multidim_arr = array(array("id"=>1374,"version_id"=>223),array("id"=>1540,"version_id"=>224),array("id"=>1541,"version_id"=>225));
echo "<pre>";print_R($multidim_arr);echo "</pre>";
$keys = array('id', 'version_id');
$rows = count($keys);
$cols = count($multidim_arr);
echo '<table border="1">';
for($j = 0; $j<$rows;$j++)
{
echo "<tr><td>$keys[$j]</td>";
for($i = 0; $i<$cols; $i++)
{
echo "<td>".$multidim_arr[$i][$keys[$j]]."</td>";
}
echo "</tr>";
}
echo "</table>";
?>