JSON PHP foreach解析问题?

时间:2016-04-13 03:03:37

标签: php json parsing foreach decoding

所以我尝试使用PHP从JSON字符串返回多个字段,但每当我尝试使用foreach两次时,第二个字段都不会显示。 JSON数据有一些返回不同数量字段的信息,我希望它能够在需要时返回1或10。到目前为止,我所拥有的代码非常适用于此,但是当我在没有任何显示之后返回另一个foreach时。如果我添加字段,我试图将第二个foreach包含在第一个foreach中,那么有时会根据第一个字段返回可能为10来创建许多额外内容。

这是PHP

foreach($decoded_results['sam_data']['registration']['qualifications']['acass']['answers'] as $acass)
{
  echo '<strong>ACASS Answer Text: </strong>' . ($acass['answerText'] ? 'Yes' : 'No') .'</br>';

  echo '<strong>ACASS Section: </strong>&nbsp&nbsp&nbsp'.$acass['section'].'</br>';

}

foreach($decoded_results ['sam_data']['registration']['qualifications']['acass']['answers']['FormerFirm'] as $formerfirm)
{

  echo '<strong>Former Firm ID: </strong>&nbsp&nbsp&nbsp'.$formerFirm['id'].'</br>';

  echo '<strong>Former Firm Year Established: </strong>&nbsp&nbsp&nbsp'.$formerFirm['yearEstablished'].'</br>';

}

这里是JSON

 "qualifications": {

        "acass": {

           "id": "SF330",

           "answers": [

              {},

              {

                 "answerText": "Yes",

                 "section": "SF330.2"

              },

              {

                 "FormerFirm": {

                    "id": 1,

                    "yearEstablished": aaaaaaaaa,

                    "name": "aaaaaaaaaaa",

                    "duns": aaaaaaaaaa

                 },

JSON结构符合资格/ acass,然后answertext和section与前公司处于同一级别。

非常感谢您提供的任何帮助。谢谢你的时间。

2 个答案:

答案 0 :(得分:4)

以下是JSON示例

{
  "sam_data": {
    "registration": {
      "govtBusinessPoc": {
        "lastName": "EAVES",
        "title": "OFFI‌​CEMANAGER",
        "address": {
          "zip": "72301",
          "countryCode": "USA",
          "line1": "207 West Bond Ave.",
          "stateorProvince": "AR",
          "city": "West Memphis"
        },
        "email": "FOWLERGAREY@GMAIL.COM",
        "usPhone": "8707356502",
        "firstName": "KE‌​LLY"
      },
      "qualifications": {
        "acass": {
          "id": "SF330",
          "answers": [
            {
              "answerText": "Yes",
              "section": "SF330.1"
            },
            {
              "answerText": "Yes",
              "section": "SF330.2"
            },
            {
              "FormerFirm": {
                "id": 1,
                "yearEstablished": 1968,
                "name": "Fowler/Garey Architects, P.A.",
                "duns": 960604007
              }
            }
          ]
        }
      }
    }
  }
}

正确显示PHP

# convert to array
# $data = json data
$decoded_results = json_decode($data, true);

$answers = $decoded_results['sam_data']['registration']['qualifications']['acass']['answers'];

foreach ($answers as $key => $acass) {

    if (isset($acass['answerText']) && isset($acass['section'])) {

        echo '<strong>ACASS Answer Text: </strong>' . ($acass['answerText']) .'</br>';

        echo '<strong>ACASS Section: </strong>&nbsp&nbsp&nbsp'.$acass['section'].'</br>';
    }
}

# get the FormerFirm
# using the array index of answers[2];
$formerfirm = $decoded_results['sam_data']['registration']['qualifications']['acass']['answers'][2]['FormerFirm'];

  echo '<strong>Former Firm ID: </strong>&nbsp&nbsp&nbsp'.$formerfirm['id'].'</br>';

  echo '<strong>Former Firm Year Established: </strong>&nbsp&nbsp&nbsp'.$formerfirm['yearEstablished'].'</br>';

输出::

ACASS Answer Text: Yes
ACASS Section:    SF330.1
ACASS Answer Text: Yes
ACASS Section:    SF330.2
Former Firm ID:    1
Former Firm Year Established:    1968

希望有所帮助

答案 1 :(得分:-2)

{
  "sam_data": {
    "registration": {
      "govtBusinessPoc": {
        "lastName": "EAVES",
        "title": "OFFI‌​CEMANAGER",
        "address": {
          "zip": "72301",
          "countryCode": "USA",
          "line1": "207 West Bond Ave.",
          "stateorProvince": "AR",
          "city": "West Memphis"
        },
        "email": "FOWLERGAREY@GMAIL.COM",
        "usPhone": "8707356502",
        "firstName": "KE‌​LLY"
      },
      "qualifications": {
        "acass": {
          "id": "SF330",
          "answers": [
            {
              "answerText": "Yes",
              "section": "SF330.1"
            },
            {
              "answerText": "Yes",
              "section": "SF330.2"
            },
            {
              "FormerFirm": {
                "id": 1,
                "yearEstablished": 1968,
                "name": "Fowler/Garey Architects, P.A.",
                "duns": 960604007
              }
            }
          ]
        }
      }
    }
  }
}
To display it properly PHP

# convert to array
# $data = json data
$decoded_results = json_decode($data, true);

$answers = $decoded_results['sam_data']['registration']['qualifications']['acass']['answers'];

foreach ($answers as $key => $acass) {

    if (isset($acass['answerText']) && isset($acass['section'])) {

        echo '<strong>ACASS Answer Text: </strong>' . ($acass['answerText']) .'</br>';

        echo '<strong>ACASS Section: </strong>&nbsp&nbsp&nbsp'.$acass['section'].'</br>';
    }
}

# get the FormerFirm
# using the array index of answers[2];
$formerfirm = $decoded_results['sam_data']['registration']['qualifications']['acass']['answers'][2]['FormerFirm'];

  echo '<strong>Former Firm ID: </strong>&nbsp&nbsp&nbsp'.$formerfirm['id'].'</br>';

  echo '<strong>Former Firm Year Established: </strong>&nbsp&nbsp&nbsp'.$formerfirm['yearEstablished'].'</br>';

解决了问题,遇到了另一个问题但是对于这个问题,这就是解决方案。谢谢Oli Soproni B!