我有一个多维数组,如下所示:
Array(
[DETROIT] => Array
(
[NORTH] => 20.00%
[SOUTH] => 30.00%
[WEST] => 25.00%
)
[CHICAGO] => Array
(
[NORTH] => 59.14%
[SOUTH] => 12.94%
[WEST] => 0.00%
[EAST] => 34.60%
)
[NEW YORK] => Array
(
[WEST] => 38.00%
[EAST] => 49.00%
)
[DALLAS] => Array
(
[WEST] => 60.57%
)
我想显示此表,以便位置位于我的表格的顶部然后 北,南,西和东都是这样的行:
DETROIT CHICAGO NEW YORK DALLAS
NORTH 20.00 59.14 N/A N/A
SOUTH 30.00 12.94 N/A N/A
WEST 25.00 0.00 38.00 60.57
EAST N/A 34.60 49.00 N/A
这是我到目前为止所做的,但我很难格式化它:
<?
foreach ($ARR as $LOCATIONS => $RESULTS) {
ECHO "{$LOCATIONS} <br>";
foreach ($RESULTS as $PERCENT) {
echo "{$PERCENT} ";
echo "<br>";
}
}
?>
答案 0 :(得分:0)
我会用一张表来显示你的信息表。很多人拒绝使用桌子,不管是什么东西,但桌子真的可以完美地处理它。
<?php
$stats = array(
"Detroit"=>array(
"Stat 1"=> "1000",
"Stat 2"=> "2000",
"Stat 3"=> "3000"
),
"Chicago"=>array(
"Stat 1"=> "4000",
"Stat 2"=> "5000",
"Stat 3"=> "6000"
)
);
echo "<table>";
$heading = "<tr><td> </td>";
$stats_key = array("Stat 1","Stat 2","Stat 3");
$cities = array();
foreach ($stats as $city=>$city_stats){
$cities[] = $city;
$heading .= "<td>" . $city . "</td>";
}
$heading .= "</tr>";
foreach ($stats_key as $key){
$table .= "<tr><td>" . $key . "</td>";
foreach ($cities as $cit){
$table .= "<td>" . $stats[$cit][$key] . "</td>";
}
$table .= "</tr>";
}
echo $heading;
echo $table;