在foreach循环中以JSON结果显示键名

时间:2018-09-27 09:53:55

标签: php json

如何在foreach循环中显示JSON结果的键名?

我尝试过:

$row = $obj->response->result->$module->row;
foreach($row->FL as $data)
  {
   echo key($data);      
  }

但是什么都不做。

这是JSON结果的示例代码段

"row": [
          {
            "no": "1",
            "FL": [
              {
                "val": "CONTACTID",
                "content": "3508588000000259007"
              },

它是我不想要的值

print_r( $obj->response->result->$module->row);

返回..

Array ( [0] => stdClass Object ( [no] => 1 [FL] => Array ( [0] => stdClass Object ( [val] => ACCOUNTID [content] => 3508588000000259001 ) [1] => stdClass Object ( [val] => SMOWNERID [content] => 3508588000000176021 ) [2] => stdClass Object ( [val] => Account Owner [content] => Chris Yates ) [3] => stdClass Object ( [val] => Rating [content] => null ) [4] => stdClass Object ( [val] => Account Name [content] => Test Co ) [5] => stdClass Object ( [val] => Phone [content] => null ) [6] => stdClass Object ( [val] => Account Site [content] => null ) [7] => stdClass Object ( [val] => Fax [content] => null ) [8] => stdClass Object ( [val] => PARENTACCOUNTID [content] => null ) [9] => stdClass Object ( [val] => Parent Account [content] => null ) [10] => stdClass Object ( [val] => Website [content] => null ) [11] => stdClass Object ( [val] => Account Number [content] => 0 ) [12] => stdClass Object ( [val] => Ticker Symbol [content] => null ) [13] => stdClass Object ( [val] => Account Type [content] => null ) [14] => stdClass Object ( [val] => Ownership [content] => null ) [15] => stdClass Object ( [val] => Industry [content] => null ) [16] => stdClass Object ( [val] => Employees [content] => 0 ) [17] => stdClass Object ( [val] => Annual Revenue [content] => 129000 ) [18] => stdClass Object ( [val] => SIC Code [content] => 0 ) [19] => stdClass Object ( [val] => SMCREATORID [content] => 3508588000000176021 ) [20] => stdClass Object ( [val] => Created By [content] => Chris Yates ) [21] => stdClass Object ( [val] => MODIFIEDBY [content] => 3508588000000176021 ) [22] => stdClass Object ( [val] => Modified By [content] => Chris Yates ) [23] => stdClass Object ( [val] => Created Time [content] => 2018-09-20 09:56:30 ) [24] => stdClass Object ( [val] => Modified Time [content] => 2018-09-20 12:39:56 ) [25] => stdClass Object ( [val] => Billing Street [content] => null ) [26] => stdClass Object ( [val] => Shipping Street [content] => null ) [27] => stdClass Object ( [val] => Billing City [content] => null ) [28] => stdClass Object ( [val] => Shipping City [content] => null ) [29] => stdClass Object ( [val] => Billing State [content] => null ) [30] => stdClass Object ( [val] => Shipping State [content] => null ) [31] => stdClass Object ( [val] => Billing Code [content] => null ) [32] => stdClass Object ( [val] => Shipping Code [content] => null ) [33] => stdClass Object ( [val] => Billing Country [content] => null ) [34] => stdClass Object ( [val] => Shipping Country [content] => null ) [35] => stdClass Object ( [val] => Description [content] => null ) [36] => stdClass Object ( [val] => Last Activity Time [content] => 2018-09-20 12:39:56 ) [37] => stdClass Object ( [val] => Tag [content] => null ) ) ) [1] => stdClass Object ( [no] => 2 [FL] => Array ( [0] => stdClass Object ( [val] => ACCOUNTID [content] => 3508588000000180068 ) [1] => stdClass Object ( [val] => SMOWNERID [content] => 3508588000000176021 ) [2] => stdClass Object ( [val] => Account Owner [content] => Chris Yates ) [3] => stdClass Object ( [val] => Rating [content] => null ) [4] => stdClass Object ( [val] => Account Name [content] => Google Inc ) [5] => stdClass Object ( [val] => Phone [content] => null ) [6] => stdClass Object ( [val] => Account Site [content] => null ) [7] => stdClass Object ( [val] => Fax [content] => null ) [8] => stdClass Object ( [val] => PARENTACCOUNTID [content] => null ) [9] => stdClass Object ( [val] => Parent Account [content] => null ) [10] => stdClass Object ( [val] => Website [content] => null ) [11] => stdClass Object ( [val] => Account Number [content] => 0 ) [12] => stdClass Object ( [val] => Ticker Symbol [content] => null ) [13] => stdClass Object ( [val] => Account Type [content] => null ) [14] => stdClass Object ( [val] => Ownership [content] => null ) [15] => stdClass Object ( [val] => Industry [content] => null ) [16] => stdClass Object ( [val] => Employees [content] => 0 ) [17] => stdClass Object ( [val] => Annual Revenue [content] => 0 ) [18] => stdClass Object ( [val] => SIC Code [content] => 0 ) [19] => stdClass Object ( [val] => SMCREATORID [content] => 3508588000000176021 ) [20] => stdClass Object ( [val] => Created By [content] => Chris Yates ) [21] => stdClass Object ( [val] => MODIFIEDBY [content] => 3508588000000176021 ) [22] => stdClass Object ( [val] => Modified By [content] => Chris Yates ) [23] => stdClass Object ( [val] => Created Time [content] => 2018-09-07 11:24:26 ) [24] => stdClass Object ( [val] => Modified Time [content] => 2018-09-10 12:52:43 ) [25] => stdClass Object ( [val] => Billing Street [content] => null ) [26] => stdClass Object ( [val] => Shipping Street [content] => null ) [27] => stdClass Object ( [val] => Billing City [content] => null ) [28] => stdClass Object ( [val] => Shipping City [content] => null ) [29] => stdClass Object ( [val] => Billing State [content] => null ) [30] => stdClass Object ( [val] => Shipping State [content] => null ) [31] => stdClass Object ( [val] => Billing Code [content] => null ) [32] => stdClass Object ( [val] => Shipping Code [content] => null ) [33] => stdClass Object ( [val] => Billing Country [content] => null ) [34] => stdClass Object ( [val] => Shipping Country [content] => null ) [35] => stdClass Object ( [val] => Description [content] => null ) [36] => stdClass Object ( [val] => Last Activity Time [content] => 2018-09-26 13:34:14 ) [37] => stdClass Object ( [val] => Tag [content] => null ) ) ) ) 

1 个答案:

答案 0 :(得分:0)

通过查看原始JSON可以清楚地看到,$row是一个数组,没有FL属性。您也可以在PHP中执行var_dump($row)来查看结构。

FL将是数组中第一项的属性。例如$row[0]->FL;那么FL本身也是一个数组。因此$row[0]->FL[0]->val应该输出您需要的内容。

实时演示:http://sandbox.onlinephpfunctions.com/code/ead7c866d5c09d24c7b9585f4e6d5ddebc184186

基于提供的示例JSON,这将输出CONTACTID

如果FL中有多个项目,并且想要全部输出,则可以使用循环。这是一个非常简单的示例,用于生成逗号分隔的列表:

foreach ($row[0]->FL as $fl)
  echo $fl->val.", ";

演示:http://sandbox.onlinephpfunctions.com/code/65efcac539bccb806f0264ba783ae7b005f892b8