[英]How to print array element with keys & values in PHP?
I'm learning array and loop in php.我正在学习 php 中的数组和循环。 But can't print the array with keys & values.
但是不能用键和值打印数组。 How can I do this?
我怎样才能做到这一点?
<?php
$marks = array (
"Alice" => array (
"physics" => "60",
"math" => "65"
),
"Bob" => array (
"physics" => "40",
"math" => "45"
)
);
foreach ( $marks as $key => $value) {
foreach ( $key as $key2 => $value2 ) {
echo $key . " : " . $key2 . " - " . $value2 . "<br>";
};
};
?>
Use this用这个
foreach ( $marks as $key => $value) {
foreach ( $value as $key2 => $value2 ) {
echo $key . " : " . $key2 . " - " . $value2 . "<br>";
}
}
This way it could be more readable to fix it and clear up confusion:这样,修复它并消除混乱可能会更具可读性:
$marks = array(
'Alice' => array(
'physics' => 60,
'math' => 65,
),
'Bob' => array(
'physics' => 40,
'math' => 45,
),
);
// Loop students
foreach($marks as $name => $grades){
// Loop their grades
foreach ($grades as $subject => $score){
echo $name . ' : ' . $subject . ' - ' . $score . '<br>';
}
}
Please note that the numbers are without quotes.请注意,数字没有引号。 This will allow you to use them as numbers to do further calculations.
这将允许您将它们用作数字来进行进一步的计算。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.