开发者

Php: problem with associative arrays

开发者 https://www.devze.com 2022-12-08 14:34 出处:网络
If i have an array with hundreds of random ids (having value开发者_运维问答s too) like (3=>23,2=>34,17=>670,5=>67...)

If i have an array with hundreds of random ids (having value开发者_运维问答s too) like

(3=>23,2=>34,17=>670,5=>67...)

how can i get an output like following via a loop

ID: 3 has a value= 23
ID: 2 has a value= 34
ID: 17 has a value= 670
ID: 5 has a value= 67

I can reference the values by their IDs like

  echo $myArray['3'];

but what if don't know in advance that what the next ID is? I mean how can i reference the IDs automatically with a loop? Is it even possible to code the following pseudo code in 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]

Need help plz...


Why not just:

foreach ($myArray as $k => $v) {
  echo "ID: $k has a value= $v\n";
}

?


foreach ($myArray as $key => $value) {
  echo "id: $key value: $value\n";
}


foreach ($myArray as $key => $value)
{
    echo "ID: $key has value of $value\n";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消