不遍历JSON结果行

时间:2018-09-26 09:50:57

标签: php json

我正在尝试简单地打印出JSON结果每一行的数据 路径结构是正确的,但它只处理第1行的数据

这是代码

$row = $obj->response->result->Accounts->row;
foreach($row as $r) 
{
print($r->FL[0]->content).',';
print($r->FL[1]->content).',';
print($r->FL[2]->content).',';
print($r->FL[3]->content).',';
print($r->FL[4]->content).',';
print($r->FL[5]->content).',';
print($r->FL[6]->content).',';
}

JSON结果 **因为人们没有正确阅读问题,所以这里是整个JSON结果

    {
  "response": {
    "result": {
      "Accounts": {
        "row": [
          {
            "no": "1",
            "FL": [
              {
                "val": "ACCOUNTID",
                "content": "3508588000000259001"
              },
              {
                "val": "SMOWNERID",
                "content": "3508588000000176021"
              },
              {
                "val": "Account Owner",
                "content": "Chris Yates"
              },
              {
                "val": "Rating",
                "content": "null"
              },
              {
                "val": "Account Name",
                "content": "Test Co"
              },
              {
                "val": "Phone",
                "content": "null"
              },
              {
                "val": "Account Site",
                "content": "null"
              },
              {
                "val": "Fax",
                "content": "null"
              },
              {
                "val": "PARENTACCOUNTID",
                "content": "null"
              },
              {
                "val": "Parent Account",
                "content": "null"
              },
              {
                "val": "Website",
                "content": "null"
              },
              {
                "val": "Account Number",
                "content": "0"
              },
              {
                "val": "Ticker Symbol",
                "content": "null"
              },
              {
                "val": "Account Type",
                "content": "null"
              },
              {
                "val": "Ownership",
                "content": "null"
              },
              {
                "val": "Industry",
                "content": "null"
              },
              {
                "val": "Employees",
                "content": "0"
              },
              {
                "val": "Annual Revenue",
                "content": "129000"
              },
              {
                "val": "SIC Code",
                "content": "0"
              },
              {
                "val": "SMCREATORID",
                "content": "3508588000000176021"
              },
              {
                "val": "Created By",
                "content": "Chris Yates"
              },
              {
                "val": "MODIFIEDBY",
                "content": "3508588000000176021"
              },
              {
                "val": "Modified By",
                "content": "Chris Yates"
              },
              {
                "val": "Created Time",
                "content": "2018-09-20 09:56:30"
              },
              {
                "val": "Modified Time",
                "content": "2018-09-20 12:39:56"
              },
              {
                "val": "Billing Street",
                "content": "null"
              },
              {
                "val": "Shipping Street",
                "content": "null"
              },
              {
                "val": "Billing City",
                "content": "null"
              },
              {
                "val": "Shipping City",
                "content": "null"
              },
              {
                "val": "Billing State",
                "content": "null"
              },
              {
                "val": "Shipping State",
                "content": "null"
              },
              {
                "val": "Billing Code",
                "content": "null"
              },
              {
                "val": "Shipping Code",
                "content": "null"
              },
              {
                "val": "Billing Country",
                "content": "null"
              },
              {
                "val": "Shipping Country",
                "content": "null"
              },
              {
                "val": "Description",
                "content": "null"
              },
              {
                "val": "Last Activity Time",
                "content": "2018-09-20 12:39:56"
              },
              {
                "val": "Tag",
                "content": "null"
              }
            ]
          },

有整个JSON结果-超过400行

1 个答案:

答案 0 :(得分:1)

您应该在FL中再次循环;

foreach($row as $r) 
{
    foreach($r->FL as $data)
    print($data->content).',';
}

在达到一定数量时停止输出逗号;

foreach($row as $r) 
{
    $data_count = count($r->FL);
    $counter = 0;

    foreach($r->FL as $data)
    {
        if($data_count < $counter)
        {
           print($data->content).',';
        }
        else
        {
           print($data->content);
        }

        $counter++;
    }

}

以换行显示(html);

foreach($row as $r) 
{
    foreach($r->FL as $data)
    print($data->content).'<br>';
}

以换行显示(excel);

我注意到的是,您必须在新行中逐字输出您的回声。 excel文件表中包含此数据

|1|2|3|4|5|
|1|2|3|4|5|

及其等效的csv文件为

1,2,3,4,5
1,2,3,4,5