dshkmamau65777662
2016-07-16 23:08
浏览 310

PHP从当前的for循环中获取Json Key

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dougezhua0017 2016-07-16 23:13
    已采纳

    Try it:

    foreach ( $decoded->rgDescriptions as $key => $desc )
      {
    
         // current key
         echo $key;
         //doing stuff with the results..
         echo $desc->itemName;
      }
    
    点赞 打赏 评论
  • dso15221 2016-07-16 23:16

    It's iteration and to get the key you can add as $key => $value to foreach params.

    foreach ( $decoded->rgDescriptions as $key => $desc )
    {
       //doing stuff with the results..
       echo $desc->itemName;
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题