0

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...

3 Answers 3

4

Why not just:

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

?

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.