如何回显多维数组的两个部分?

我遇到的问题是当我遍历多维数组时,一旦我掌握了多维数组的技能,它就会打印出: 

Andrew Wiley

30

Game Designer

72000

Array

C++ Level Design Leadership


它打印出 Array 和 C++ Level Design Leadership。


如何删除 Array 的输出并将其替换为技能 C++ Level Design Leadership 而不打印两者?


$students = [

    Andrew => [

        fullName => Andrew Wiley,

        age => 30,

        jobTitle => Game Designer,

        Salary => 72000,

        skills => [C++, Level Design, Leadership]

    ]

];


foreach($students[Andrew] as $student) {

    echo $student . <br>;

    if($student == $students[Andrew][skills]) {

        foreach($students[Andrew][skills] as $skill) {

            echo $skill;

        }

    }

};


忽然笑
浏览 174回答 2
2回答

江户川乱折腾

您可以使用递归函数。function printVariables($array,$level=1){&nbsp; &nbsp; foreach($array as $value){&nbsp; &nbsp; &nbsp; &nbsp; if(is_array($value)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $level++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printVariables($value,$level);&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $level <= 2 ? "$value\n" : "$value ";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}printVariables($students);或者序列化...foreach($students as $student){&nbsp; &nbsp; &nbsp;echo json_encode($student,JSON_PRETTY_PRINT);}

慕桂英546537

像这样更新你的 foreach:foreach($students['Andrew'] as $student) {&nbsp; &nbsp; echo $student . <br>;&nbsp; &nbsp; &nbsp; if($student === $students['Andrew']['skills']&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; && is_array($students['Andrew']['skills'])&nbsp; &nbsp; &nbsp; &nbsp; && count($students['Andrew']['skills']) > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo implode(", ",$students['Andrew']['skills']);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;}输出:C++、关卡设计、领导力这是做什么的:如果语句检查它是否相等,然后检查它是否是一个数组然后检查数组不为空在最终将所有数组元素连接成一个字符串并用echo.还要注意数组键周围的引号
打开App,查看更多内容
随时随地看视频慕课网APP