我正在尝试创建一个多维关联数组并显示其所有值。
如何使用for循环?
类似的代码:
$StudScore = array(
"Mary" => array(
"physics" => 35,
"maths" => 30,
"chemistry" => 39
),
"Tom" => array(
"physics" => 30,
"maths" => 32,
"chemistry" => 29
),
"Jon" => array(
"physics" => 31,
"maths" => 22,
"chemistry" => 39
)
);
答案 0 :(得分:1)
看看这个递归函数,看它是否合适:
function echoArray($array) {
foreach ($array as $key => $value) {
echo "<li>$key</li>";
if (is_array($value)) {
echo "<ul>";
echoArray($value);
echo "</ul>";
} else {
echo "<ul><li>$value</li></ul>";
}
}
}
根据需要更改显示每个项目的方式。