iraphi iraphi - 4 months ago 12
JSON Question

PHP get Json Key from current for-loop

I'm pretty new to PHP and stuff and I've come to this problem:

I'm looping through a few JSON entries to get their data, but the problem is, I need the Key from the current iritation.

For example:

foreach ( $decoded->rgDescriptions as $desc )
{

//doing stuff with the results..
echo $desc->itemName;
}


But what I need is this:

Image with an arrow pointing to a key-name of the json file

While irritating through the json I really need the key because it contains an unique Id given to the data recieved in the irritation.

Sadly, through the way the json was given the acutal data doesn't contain the unique Id again, so my only choice is to get it from the json - key.

So, what I need to now, how can I get the current Key from the json array that I'm looping through?

Thanks for any help, please don't go too hard on me :p

Answer

Try it:

foreach ( $decoded->rgDescriptions as $key => $desc )
  {

     // current key
     echo $key;
     //doing stuff with the results..
     echo $desc->itemName;
  }
Comments