使用PHP的foreach循环
使用foreach
foreach是一个用于循环迭代的关键字。
当需要显示数组中的多个值时使用。
$members = [
'name' => '鈴木',
'height' => 172,
'hobby' => '野球'
];
foreach($members as $member){
echo $member;
}
$members = [
'name' => '鈴木',
'height' => 172,
'hobby' => '野球'
];
//キーとバリューそれぞれ表示
foreach($members as $member => $value){
echo $member . 'は' . $value;
}
$members_1 = [
'鈴木' => [
'height' => 172,
'hobby' => '野球'
],
'田中' => [
'height' => 168,
'hobby' => '野球'
]
];
}
在这种情况下会怎样呢?
foreach($members_1 as $member){
echo $member;
}
//多段階の配列を展開
foreach($members_1 as $member_1){
foreach($member_1 as $member){
echo $member;
}
}