[英]How to print out keys and values of one array based on keys and values of another 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
這是我用於打印下表的代碼:
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循環讓我重復了很多。 同樣,沒有兩個人可以掌管一個地方。
代碼中的最后一個foreach
foreach ($cities as $cit){
foreach($person_name as $charge){
if($charge = $cit){
$table .= "<td>" . $stats[$cit][$key] . "</td>";
break;
}else{
$table .= "<td> N/A </td>";
}
}
據我了解,您需要以與第二個數組中顯示的順序相同的順序顯示城市。
嘗試這個:
$array1 = 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%',
),
);
$array2 = array(
'John Doe' => array(
'DETROIT',
'DALLAS',
),
'Sara Smith' => array(
'NEW YORK',
),
'Donald Duck' => array(
'CHICAGO',
),
);
function get2ndLevel($arr, $bKeys = false) {
$vals = array();
array_walk($arr, function($v, $k) use(&$vals, $bKeys){
$vals = array_merge($vals, $bKeys? array_keys($v) : array_values($v));
});
return array_values(array_unique($vals));
}
$dirs = get2ndLevel($array1, true);
$cities = get2ndLevel($array2);
$cities = array_merge($cities,
array_values(array_diff(array_keys($array1), $cities))
);
$table = "<tr><td> </td>";
foreach ($cities as $city){
$table .= "<td>" . $city . "</td>";
}
$table .= "</tr>";
foreach ($dirs as $dir){
$table .= "<tr><td>" . $dir . "</td>";
foreach ($cities as $city){
$table .= "<td>" . (isset($array1[$city][$dir])? $array1[$city][$dir] : "N/A") . "</td>";
}
$table .= "</tr>";
}
echo "<table>". $table. "</table>";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.