1

I have this JSON output from a Government API, I need to display it using PHP. The problem is I can't use foreach more then once in a row or it doesn't work. I can't load all the criteria into the first foreach because say the first piece of data ACASS returns 3 results, all the fields after it will be displayed 3 times. Each field could return 1-10 results so there needs to be a system that accounts for variables.

I'm thinking the solution is to put all of the JSON items I need displayed into the first foreach but set them to only display if they're populated. That or use the current coding system I have but account for variable numbers somehow.

Any potential solutions are greatly appreciated.

This is the JSON output... https://api.data.gov/sam/v4/registrations/9606040070000?api_key=WI7nHENlp6QDMnWsb0Nnmzsv1slPDTjNM0XBoKvY

Here's the PHP I'm using...

echo "ACASS ID:".$decoded_results['sam_data']['registration']['qualifications']['acass']['id']."</br>"; 

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

  {

  echo 'Answer Text:'.$acass['answerText'].'</br>';
  echo 'ACASS Section:'.$acass['section'].'</br>';

  }

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

  echo 'Former Firm ID:'.$formerfirm['id'].'</br>';
  echo 'Former Firm Year Established:'.$formerfirm['yearEstablished'].'</br>';
  echo 'Former Firm Name:'.$formerfirm['name'].'</br>';
  echo 'Former Firm DUNS'.$formerfirm['duns'].'</br>';

I did my best to keep this short and simple question / code wise. In summary the issue is if you look at the JSON the data hierarchy makes a lot of the information display under ACASS/Answers and then the next category. I never know how many responses there will be and I'm not sure how to account for those variables.

I would like to thank everyone on these boards who has guided me as a new member and helped me post cleaner, more concise questions. Also thank you to everyone who has taken their own personal time to help me learn to become a better programmer.

John Chase
  • 105
  • 8
  • 2
    are you sure you want all that data on here, including the API key in the url? – Pevara Apr 14 '16 at 18:08
  • have you remembered to pass the second parameter before working the result as an array? ex: `$decoded_results = json_decode($jsonString, true);` – CarlosCarucce Apr 14 '16 at 18:09
  • The data is publicly available and the API key is one I got just for this purpore, thank you though. I appreciate you looking out for me. Also Carlos I'm not sure I follow you, I thought I did that by writing formerfirm [2]. I skipped the second parameter, just that the way I have it doesn't account for the fact there could be more than one or two or even 10 returned results in that position. – John Chase Apr 14 '16 at 18:12
  • I'll be honest the only PHP I've ever used was to create WordPress sites, this week I jumped into using it for this API so I only have a very basic understanding of it. I've been trying to do some reading of my own but when I search for information on this topic I'm having trouble phrasing my problem. – John Chase Apr 14 '16 at 18:13
  • The second parameter instructs to decode into an associative array (which you are trying to access later). If you skip it, [json_decode](http://php.net/manual/en/function.json-decode.php) will default false and product a stdObj (which you dont seem to want). – YvesLeBorg Apr 14 '16 at 18:15

1 Answers1

2

use a tool like http://jsonviewer.stack.hu/ for visualizing your json structure. It helps a lot.

 <?php

$url = "https://api.data.gov/sam/v4/registrations/9606040070000?api_key=WI7nHENlp6QDMnWsb0Nnmzsv1slPDTjNM0XBoKvY";

$contents = json_decode(file_get_contents($url)); 

// echo var_dump($contents); 

$sam_data = $contents->sam_data; 

// echo var_dump($sam_data); 

$registration = $sam_data->registration; 

//echo var_dump($registration); 
$acass = $contents->sam_data->registration->qualifications->acass; 
$id = $acass->id; 

echo "id:  ". $id . "<br />";

//echo var_dump($acass->answers); 

foreach($acass->answers as $answer) {

    if(isset($answer->FormerFirm)) {
        $formerFirm = $answer->FormerFirm;      
        echo var_dump($formerFirm);
    }

} 
marko
  • 10,684
  • 17
  • 71
  • 92
  • Remember that you must allow external file reading before using `file_get_contents` with an url, as described here: http://stackoverflow.com/a/3488430/3435728 – CarlosCarucce Apr 14 '16 at 18:18
  • @marko ah ! a nice tool, straight into my bookmarks. Thanks – YvesLeBorg Apr 14 '16 at 18:19
  • I could continue to structure it from that point on? Does it matter how many returns results each element has? For instance if 5 ACASS results are returned will the next category of nested JSON information under it (formerfirm) be off? I'm just trying to understand how this code works instead of just cutting and pasting it and never knowing. – John Chase Apr 14 '16 at 18:21
  • Check how to get answers also and FormerFirm. – marko Apr 14 '16 at 18:27