Reputation: 9837
If i have an array with hundreds of random ids (having values 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...
Upvotes: 0
Views: 107
Reputation: 31290
foreach ($myArray as $key => $value) {
echo "id: $key value: $value\n";
}
Upvotes: 3
Reputation: 53921
foreach ($myArray as $key => $value)
{
echo "ID: $key has value of $value\n";
}
Upvotes: 2
Reputation: 625337
Why not just:
foreach ($myArray as $k => $v) {
echo "ID: $k has a value= $v\n";
}
?
Upvotes: 4