PHP从当前for循环中获取Json密钥


PHP get Json Key from current for-loop

我对PHP和其他东西很陌生,我遇到了这个问题:

我正在循环浏览一些JSON条目以获取它们的数据,但问题是,我需要来自当前目录的Key。

例如:

  foreach ( $decoded->rgDescriptions as $desc )
  {
     //doing stuff with the results..
     echo $desc->itemName;
  }

但我需要的是:

带有指向json文件密钥名称的箭头的图像

虽然json让我很恼火,但我确实需要密钥,因为它包含一个唯一的Id,用于在json中接收到的数据。

遗憾的是,通过给json的方式,实际数据不再包含唯一的Id,所以我唯一的选择是从json-key中获取它。

那么,我现在需要做的是,如何从我正在循环的json数组中获取当前Key?

谢谢你的帮助,请不要对我太苛刻:p

试试看:

foreach ( $decoded->rgDescriptions as $key => $desc )
  {
     // current key
     echo $key;
     //doing stuff with the results..
     echo $desc->itemName;
  }

这是一次迭代,为了获得密钥,您可以将$key=>$value添加到foreach参数中。

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