![](/img/trans.png)
[英]How to update values in associative array using php foreach loop when key includes a specific string?
[英]php - how to loop through values of one specific key in an associative array?
我是php新手 ,我有一个像这样的关联数组
$arr['Joe'] = "test1";
$arr['Joe'] = "test2";
$arr['Joe'] = "test3";
如何遍历这个特定键Joe
所有值test1, test2, test3
?
我试过这个
foreach ($arr as $key => $value) {
echo $value;
}
和这个
foreach ($arr as $key => $value) {
echo $arr [$key]['Joe'];
}
但没什么! 请帮我?
我想这就是你想要的:
<?php
$arr['Joe'][] = "test1";
$arr['Joe'][] = "test2";
$arr['Joe'][] = "test3";
foreach ($arr['Joe'] as $key => $value) {
echo $value;
}
?>
通过在['Joe']之后添加[],值将保存为:
(
[Joe] => Array
(
[0] => test1
[1] => test2
[2] => test3
)
)
要在关联数组中保存多个值,它们每个都必须是唯一的。 例如。
$arr['Joe1'] = "test1";
$arr['Joe2'] = "test2";
$arr['Joe3'] = "test3";
接着
foreach ($arr as $value) {
echo $value;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.