我有一个看起来像这样的数组。
[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循环给我带来了太多重复。此外,没有两个人可以负责一个地方。
答案 0 :(得分:0)
代码中的最后一个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>";
}
}
答案 1 :(得分:0)
据我了解,您需要按照第二个数组中显示的顺序显示城市。
试试这个:
$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>";