我有一個看起來像這樣的數組。如何根據另一個數組的鍵和值打印出一個數組的鍵和值?
[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
這是我爲下表打印出來代碼:
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;
我有另一個數組,鍵是負責這些地區的人民。現在
[John Doe] => Array
(
[0] => DETROIT
[1] => DALLAS
)
[Sara Smith] => Array
(
[1] => NEW YORK
)
[Donald Duck] => Array
(
[0] => CHICAGO
)
,我只是想打印出來的同一個表,但是這是同一人下的位置扎堆打印出來,像這樣:
DETROIT DALLAS NEW YORK CHICAGO
NORTH 20.00 N/A N/A 59.14
SOUTH 30.00 N/A N/A 12.94
WEST 25.00 60.57 38.00 0.00
EAST N/A N/A 49.00 34.60
任何幫助,將不勝感激,謝謝。
編輯: 這是我迄今所做的:
echo "<table>";
$heading = "<tr><td> </td>";
$stats_key = array("Stat 1","Stat 2","Stat 3");
$cities = array();
foreach($PERSON as $per){
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;
}
}
但是,額外的foreach循環得到了我太多的重複。另外,沒有兩個人可以負責一個地方。
所以你能證明你做了什麼來嘗試這些列聚集在一起? –
不止一個人可以負責一個地方? – theB3RV
什麼樣的重複? – theB3RV