1

So I'm trying to return multiple fields from a JSON string using PHP but whenever I try to use foreach twice the second one doesn't display. The JSON data has some information which returns a varying numbers of fields and I want it to be able to return 1 or 10 if needed. The code I have so far works great for that, but when I go to return another foreach after it nothing shows up. If I add the fields I'm trying to include in the second foreach to the first then sometimes many extras are created based off the first fields returns being possibly 10.

Here's the 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>';

}

Here's the JSON

 "qualifications": {

        "acass": {

           "id": "SF330",

           "answers": [

              {},

              {

                 "answerText": "Yes",

                 "section": "SF330.2"

              },

              {

                 "FormerFirm": {

                    "id": 1,

                    "yearEstablished": aaaaaaaaa,

                    "name": "aaaaaaaaaaa",

                    "duns": aaaaaaaaaa

                 },

The JSON structure goes qualifications / acass and then answertext and section are on the same level as former firm.

Any help you guys provide is greatly appreciated. Thank you for your time.

4
  • Kindly post the valid ** JSON ** you are decoding Commented Apr 13, 2016 at 3:11
  • {"sam_data":{"registration":{"govtBusinessPoc":{"lastName":"EAVES","title":"OFFICE MANAGER","address":{"zip":"72301","countryCode":"USA","line1":"207 West Bond Ave.","stateorProvince":"AR","city":"West Memphis"},"email":"[email protected]","usPhone":"8707356502","firstName":"KELLY"},"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} Commented Apr 13, 2016 at 3:17
  • @JohnChase the Json you have provided above is not a valid json in the first place to decode it Commented Apr 13, 2016 at 3:33
  • Here's the return information... api.data.gov/sam/v4/registrations/… Commented Apr 13, 2016 at 3:59

2 Answers 2

3

Here is a sample 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": "[email protected]",
        "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>';

Output ::

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

Hope it help

Sign up to request clarification or add additional context in comments.

5 Comments

Is this code missing brackets or characters? When I input it into my php and go to load the page nothing happens. Thank you for taking the time to answer though!
the json that was provided is a invalid json so i fixed it, there are missing closing brackets
Hmm, so I tried the code and it works. I appreciate that alot but another issue I hit was the fact if I repeat that code to parse items below the ones I cited they work as well but only if one set of data per item is returned. If more than one returns it doesn't work.
Is there anyway to change the [2] to a wildcard or variable?
Its up to your json data creation already, on your 2nd foreach, the problem is that you are iterating the whole object properties, but you only want to display the properties of the object at a single time. I suggest you need to redesign your json data to fit your need
-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": "[email protected]",
        "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>';

Solved the problem, ran into another issue but for this problem this was the solution. Thanks Oli Soproni B!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.