dshkmamau65777662 2016-07-16 23:08
浏览 374
已采纳

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;
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 maixpy训练模型,模型训练好了以后,开发板通电会报错,不知道是什么问题
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容