我有一个多维数组,我想按如下方式打印内容:
Sam
class1: 76
class2: 62
class3: 56
class4: 60
Matt
class1: 76
class2: 62
class3: 56
class4: 60
Dave
class1: 76
class2: 62
class3: 56
class4: 60
Steve
class1: 76
class2: 62
class3: 56
class4: 60
我的问题是我不知道如何引用第一个子数组的键(名称)。 key($index)
只是用“class1”替换输出中的所有名称。到目前为止,这是我的代码:
$scoreList = array(
"Sam" => array(
"class1" => 76,
"class2" => 62,
"class3" => 56,
"class4" => 60,
),
"Matt" => array(
"class1" => 76,
"class2" => 62,
"class3" => 56,
"class4" => 60,
),
"Dave" => array(
"class1" => 76,
"class2" => 62,
"class3" => 56,
"class4" => 60,
),
"Steve" => array(
"class1" => 76,
"class2" => 62,
"class3" => 56,
"class4" => 60,
));
printGrade($scoreList["Sam"]);
printGrade($scoreList["Matt"]);
printGrade($scoreList["Dave"]);
printGrade($scoreList["Steve"]);
function printGrade($index) {
Print key($index)."<br>";
for($i = 1; $i <= 4; $i++)
{
Print "class".$i.": ".$index["class".$i]."<br>";
};
echo "<br>";
}
我对网络开发很陌生,所以如果有人有任何提示,我会非常感激!
编辑:我越来越近了吗??
function printGrade($index) {
foreach ($index as &$value) {
print array_keys($index).": ".$value."<br>";
}
echo "<br>";
}
答案 0 :(得分:1)
这可以通过递归函数调用来完成:
function printGrade($scoreList) {
foreach($scoreList as $key=>$score) {
echo $key;
if(is_array($score)) {
echo "<br/>";
printGrade($score);
} else {
echo ": $score<br/>";
}
}
}
答案 1 :(得分:0)
试试这个:
foreach($scoreList as $key => $val)
{
echo $key."</br>";
foreach( $val as $keyItem => $valKey)
{
echo $keyItem ." : ".$valKey."</br>";
}
}
Out put:
Sam
class1 : 76
class2 : 62
class3 : 56
class4 : 60
Matt
class1 : 76
class2 : 62
class3 : 56
class4 : 60
Dave
class1 : 76
class2 : 62
class3 : 56
class4 : 60
Steve
class1 : 76
class2 : 62
class3 : 56
class4 : 60
答案 2 :(得分:0)
<<!DOCTYPE html>
<html>
<body>
<?php
$scoreList = array(
"Sam" => array(
"class 1" => 76,
"class 2" => 62,
"class 3" => 56,
"class 4" => 60,
),
"Matt" => array(
"class 1" => 76,
"class 2" => 62,
"class 3" => 56,
"class 4" => 60,
),
"Dave" => array(
"class 1" => 76,
"class 2" => 62,
"class 3" => 56,
"class 4" => 60,
),
"Steve" => array(
"class 1" => 76,
"class 2" => 62,
"class 3" => 56,
"class 4" => 60,
));
echo "Sam: ". "<br>"."<br>";
echo "class 1:  ". $scoreList ['Sam'] ['class 1']. "<br>";
echo "class 2:  ". $scoreList ['Sam'] ['class 2']. "<br>";
echo "class 3:  ". $scoreList ['Sam'] ['class 3']. "<br>";
echo "class 4:  ". $scoreList ['Sam'] ['class 4']. "<br>"."<br>";
echo "Matt: ". "<br>"."<br>";
echo "class 1:  ". $scoreList ['Matt'] ['class 1']. "<br>";
echo "class 2:  ". $scoreList ['Matt'] ['class 2']. "<br>";
echo "class 3:  ". $scoreList ['Matt'] ['class 3']. "<br>";
echo "class 4:  ". $scoreList ['Matt'] ['class 4']. "<br>"."<br>";
echo "Dav: ". "<br>"."<br>";
echo "class 1:  ". $scoreList ['Dave'] ['class 1']. "<br>";
echo "class 2:  ". $scoreList ['Dave'] ['class 2']. "<br>";
echo "class 3:  ". $scoreList ['Dave'] ['class 3']. "<br>";
echo "class 4:  ". $scoreList ['Dave'] ['class 4']. "<br>"."<br>";
echo "Steve: ". "<br>"."<br>";
echo "class 1:  ". $scoreList ['Steve'] ['class 1']. "<br>";
echo "class 2:  ". $scoreList ['Steve'] ['class 2']. "<br>";
echo "class 3:  ". $scoreList ['Steve'] ['class 3']. "<br>";
echo "class 4:  ". $scoreList ['Steve'] ['class 4']. "<br>"."<br>";
?>
</body>
</html>
答案 3 :(得分:0)
仅使用 one foreach 并回显所有内部数组 ($val
) 作为 key: value
对,使用 implode(glue, pieces)
其中 < em>glue 是 </br>
新行。为了打印 key: value
,我使用了 array_map
和回调 fn
返回格式 $k: $v
,其中 $k
和 $v
来自 array_keys
和array_values
分别。
foreach($scoreList as $key => $val)
{
echo $key."</br>";
echo implode("</br>", array_map(
fn($k,$v)=>"$k: $v",
array_keys($val),
array_values($val)
)
);
echo "</br></br>";
}
输出正如你期望
Sam
class1: 76
class2: 62
class3: 56
class4: 60
Matt
class1: 76
class2: 62
class3: 56
class4: 60
Dave
class1: 76
class2: 62
class3: 56
class4: 60
Steve
class1: 76
class2: 62
class3: 56
class4: 60