![](/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.