如果我有一个包含数百个随机id(也有值)的数组,比如
(3=>23,2=>34,17=>670,5=>67...)
如何通过循环获得输出如下
ID: 3 has a value= 23
ID: 2 has a value= 34
ID: 17 has a value= 670
ID: 5 has a value= 67
我可以通过ID来引用这些值,例如
echo $myArray['3'];
但如果事先不知道下一个ID是什么呢?我的意思是如何通过循环自动引用ID?甚至可以在php中编写以下伪代码吗?
myArray's first location's ID has value = $myArray[$myArray's first location item]
myArray's 2nd location's ID has value = $myArray[$myArray's 2nd location item]
需要帮助PLZ ......
答案 0 :(得分:4)
为什么不呢:
foreach ($myArray as $k => $v) {
echo "ID: $k has a value= $v\n";
}
答案 1 :(得分:3)
foreach ($myArray as $key => $value) {
echo "id: $key value: $value\n";
}
答案 2 :(得分:2)
foreach ($myArray as $key => $value)
{
echo "ID: $key has value of $value\n";
}