[英]php - how to loop through values of one specific key in an associative array?
I am new to php , I have an associative array like this 我是php新手 ,我有一个像这样的关联数组
$arr['Joe'] = "test1";
$arr['Joe'] = "test2";
$arr['Joe'] = "test3";
how do I loop through all the values test1, test2, test3
of this specific key Joe
? 如何遍历这个特定键
Joe
所有值test1, test2, test3
?
I tried this 我试过这个
foreach ($arr as $key => $value) {
echo $value;
}
and this 和这个
foreach ($arr as $key => $value) {
echo $arr [$key]['Joe'];
}
But nothing ! 但没什么! Please help me?
请帮我?
I think this is what you want: 我想这就是你想要的:
<?php
$arr['Joe'][] = "test1";
$arr['Joe'][] = "test2";
$arr['Joe'][] = "test3";
foreach ($arr['Joe'] as $key => $value) {
echo $value;
}
?>
By adding [] after ['Joe'] the values will be saved like this: 通过在['Joe']之后添加[],值将保存为:
(
[Joe] => Array
(
[0] => test1
[1] => test2
[2] => test3
)
)
To hold multiple values in an associative array they each need to be unique. 要在关联数组中保存多个值,它们每个都必须是唯一的。 eg.
例如。
$arr['Joe1'] = "test1";
$arr['Joe2'] = "test2";
$arr['Joe3'] = "test3";
and then 接着
foreach ($arr as $value) {
echo $value;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.