我有:
$l = array(
array("A"=>0.1,"B"=>1,"C"=>1,"D"=>1),
array("A"=>0.1,"B"=>1,"C"=>0,"D"=>2),
);
$h = array('h1','h2');
1 - 我如何map(l,h)
这个?
$result= $array(
'h1'=> array("A"=>0.1,"B"=>1,"C"=>1,"D"=>1),
'h1'=> array("A"=>0.1,"B"=>1,"C"=>0,"D"=>2),
);
2-所以我可以显示(现在的html表格)
-------------------
| A | B | C | D
-------------------
h1 |
-------------------
h2 |
--------------------
我试图输出:
<table>
<tr><td>A</td><td>B</td><td>C</td><td>D</td></tr>
foreach($result as $key=>$value){
<tr>
<tr>
}
<table>
任何人都可以帮助我?
答案 0 :(得分:6)
按照您的建议映射数组很简单:
$mapped = array_combine($h, $l);
然后:
// Print the top "headers" row
$columns = array_keys(reset($l));
echo '<table><tr><td> </td>';
foreach ($columns as $column) {
echo '<td>'.$column.'</td>';
}
echo '</tr>';
// Print each data row
foreach ($mapped as $key => $row) {
echo '<tr><td>'.$key.'</td>';
foreach ($row as $cell) {
echo '<td>'.$cell.'</td>';
}
echo '</tr>';
}
// Done!
echo '</table>';
答案 1 :(得分:0)
$result = array_combine($h, $l);
这适用于bc数组元素有隐含的数字索引,因为您可以看到var_dump($h)
或var_dump($l)
<table>
<?php
echo "<tr>";
echo "<td> </td>";
foreach(array_keys($l[0]) as $letter)
echo "<td>$letter</td>"; //A,B,C,D
echo "</tr>";
foreach($result as $h_key=>$innerArr)
{
echo "<tr><td>$hkey</td>"; //h1,h2
foreach($innerArr as $key=>$val)
echo "<td>$val</td>"; //0.1, 1, etc.
echo "</tr>";
}
?>
</table>