[英]How to echo out two parts of a Multidimensional Array?
The problem I am getting is when I loop through the multidimensional array, once I get to skills in the multidimensional array, it prints out: Andrew Wiley 30 Game Designer 72000 Array C++ Level Design Leadership
我得到的问题是当我循环遍历多维数组时,一旦我掌握了多维数组中的技能,就打印出来:
Andrew Wiley 30 Game Designer 72000 Array C++ Level Design Leadership
It's printing out both Array and C++ Level Design Leadership. 它打印出阵列和C ++级别设计领导力。
How do I remove the output of Array and replace it with the skills C++ Level Design Leadership without printing both? 如何删除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;
}
}
};
You can use a recursive function. 您可以使用递归函数。
function printVariables($array,$level=1){
foreach($array as $value){
if(is_array($value)){
$level++;
printVariables($value,$level);
}else{
echo $level <= 2 ? "$value\n" : "$value ";
}
}
}
printVariables($students);
Or serialize it... 或者序列化......
foreach($students as $student){
echo json_encode($student,JSON_PRETTY_PRINT);
}
Update your foreach like so: 像这样更新你的foreach:
foreach($students['Andrew'] as $student) {
echo $student . <br>;
if($student === $students['Andrew']['skills']
&& is_array($students['Andrew']['skills'])
&& count($students['Andrew']['skills']) > 0) {
echo implode(", ",$students['Andrew']['skills']);
}
}
Output: 输出:
C++, Level Design, Leadership
C ++,关卡设计,领导力
What this does: 这是做什么的:
echo
. echo
输出该字符串之前。 Also note the quotes around the array keys 还要注意数组键周围的引号
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.