hi!
this funtion I just written and used
will display a 2-dimension array
in the format of database rows
It will display a table header, using the names of the columns/fields.
I put in the array I have tested it on.
<?php
// display a 2-dimensional array
// = an array of arrays by format of database result
function display_array($array){
$reset = reset($array);
echo '<style>td,th{background-color:white}</style>';
echo '<table cellspacing="1" style="background-color:black">';
echo '<tr><th>key</th>';
foreach($reset AS $field =>$x)
echo '<th>'.$field.'</th>';
echo '</tr>';
foreach($array AS $key => $row){
echo '<tr>';
echo '<td>'.$key.'</td>';
foreach($row AS $value)
echo '<td>'.$value.'</td>';
echo '</tr>';
}
echo '</table>';
}
//Can also be 'without' keynames
$data = array(
array("age"=>25, "name"=>"Boney", "rating"=>7),
array("age"=>32, "name"=>"Killer", "rating"=>1),
array("age"=>30, "name"=>"Ghost", "rating"=>2),
array("age"=>20, "name"=>"Tuck", "rating"=>5)
);
// My array
$data = array(
3 => array("age"=>25, "name"=>"Boney", "rating"=>7),
5 => array("age"=>32, "name"=>"Killer", "rating"=>1),
6 => array("age"=>30, "name"=>"Ghost", "rating"=>2),
8 => array("age"=>20, "name"=>"Tuck", "rating"=>5)
);
// display a 2-dimensional array
// = an array of arrays by format of database result rows
display_array( $data );
?>
I will post a screenshot.
🙂