在 PHP 多维数组中打印一个键

Print a key in a PHP multidimensional array

我有一个多维数组,想打印内容如下:

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>";
}

这可以通过递归函数调用来完成:

function printGrade($scoreList) {
    foreach($scoreList as $key=>$score) {
        echo $key;
        if(is_array($score)) {
            echo "<br/>";
            printGrade($score);
        } else {
            echo ": $score<br/>";
        }
    }
}

试试这个:

foreach($scoreList as $key => $val)
{
    echo $key."</br>";
    foreach( $val as $keyItem => $valKey)
    {
        echo $keyItem ." : ".$valKey."</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
<<!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:&nbsp&nbsp". $scoreList ['Sam'] ['class 1']. "<br>";
 echo "class 2:&nbsp&nbsp". $scoreList ['Sam'] ['class 2']. "<br>";
 echo "class 3:&nbsp&nbsp". $scoreList ['Sam'] ['class 3']. "<br>";
 echo "class 4:&nbsp&nbsp". $scoreList ['Sam'] ['class 4']. "<br>"."<br>";

 echo "Matt: ". "<br>"."<br>";
 echo "class 1:&nbsp&nbsp". $scoreList ['Matt'] ['class 1']. "<br>";
 echo "class 2:&nbsp&nbsp". $scoreList ['Matt'] ['class 2']. "<br>";
 echo "class 3:&nbsp&nbsp". $scoreList ['Matt'] ['class 3']. "<br>";
 echo "class 4:&nbsp&nbsp". $scoreList ['Matt'] ['class 4']. "<br>"."<br>";

 echo "Dav: ". "<br>"."<br>";
 echo "class 1:&nbsp&nbsp". $scoreList ['Dave'] ['class 1']. "<br>";
 echo "class 2:&nbsp&nbsp". $scoreList ['Dave'] ['class 2']. "<br>";
 echo "class 3:&nbsp&nbsp". $scoreList ['Dave'] ['class 3']. "<br>";
 echo "class 4:&nbsp&nbsp". $scoreList ['Dave'] ['class 4']. "<br>"."<br>";

 echo "Steve: ". "<br>"."<br>";
 echo "class 1:&nbsp&nbsp". $scoreList ['Steve'] ['class 1']. "<br>";
 echo "class 2:&nbsp&nbsp". $scoreList ['Steve'] ['class 2']. "<br>";
 echo "class 3:&nbsp&nbsp". $scoreList ['Steve'] ['class 3']. "<br>";
 echo "class 4:&nbsp&nbsp". $scoreList ['Steve'] ['class 4']. "<br>"."<br>";

?>

</body>
</html>

仅使用 one foreach 回显所有内部数组 ($val) 作为 key: value 对,使用 implode(glue, pieces) 其中 glue</br> 新行。为了打印 key: value,我使用 array_map 和回调 fn return 格式 $k: $v,其中 $k$v 来自 array_keysarray_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