<?php
$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
)
);
foreach ($StudScore as $key => $value) {
echo "<li>$key</li>";
if (is_array($value)) {
echo "<ul>";
echoArray($value);
echo "</ul>";
} else {
echo "<ul><li>$value</li></ul>";
}
}
?>
到目前为止,我一直尝试打印数组,但是遇到错误,请帮我解决这个问题。
输出: 玛丽 致命错误:调用未定义的函数echoArray()
我希望将其打印为示例:
玛丽=物理学:35,数学:30,化学:39
Tom =物理:30,数学:32,化学:29
Jon =物理:31,数学:22,化学:39
答案 0 :(得分:0)
如果要打印整个数组,则必须为echo var_dump($value);
或echo print_r($value);
。 PHP中没有名为echoArray()
的函数。
当您只想从数组中打印一个键/值时,您还必须使用echo $value;
,因为键/值只是字符串。
请参见https://www.php.net/manual/en/function.var-dump.php和https://www.php.net/manual/en/function.print-r.php
答案 1 :(得分:0)
<?php
foreach ($StudScore as $name => $value) {
echo $name." = ";
foreach ($value as $subject => $marks) {
echo $subject.":".$marks.",";
}
echo "<br/>";
}
?>
最后我得到了答案,它给出了如下输出:
玛丽=物理学:35,数学:30,化学:39
Tom =物理:30,数学:32,化学:29
Jon =物理:31,数学:22,化学:39