tim.baker
tim.baker

Reputation: 3307

PHP API Get Node

I am getting a API response as such:

"alert": "success",
"content": [
    {
        "id": "HIDDEN",
        "answers": {
            "3": {
                "text": "Name",
                "type": "control_textbox",
                "answer": "Tim"
            },
            "4": {
                "text": "Telephone Number",
                "type": "control_textbox",
                "answer": "Sample"
            }
   }

Whilst I can get "alert" for example I am not sure how to get each of the "TEXT" responses

I have tried this:

foreach ($submissions as $submissions) {
      print "<p class='list'><b>" . $submissions["alert"] . "</b><br>"; $answer['text'];

}

Just to be clear the code above is returning the ALERT content but not TEXT. I understand this is because it is in an array but I can't get that returned, nor can I find any responses similar to this in JotForm or find the answer by reading up on APIs!

Upvotes: 0

Views: 63

Answers (2)

Amal Murali
Amal Murali

Reputation: 76636

If you want an associative array as the result, you can specify the second parameter for json_decode() as `TRUE:

$submissions = json_decode($yourJSONstring, TRUE);

Now, you'll have an associative array in $submissions. We just need to loop through the array:

foreach ($submissions['content'][0]['answers'] as $submission) {
    print $submission["text"]."<br/>"; 
}

Output:

Name
Telephone Number

Similarly, you can access other elements too.

See it live!

Upvotes: 0

Tim Withers
Tim Withers

Reputation: 12059

I am not sure how you are processing it, but if you use $submission= json_decode($api_response, true); you will get a multi-dimensional array, which you can loop through or access however you would like.

$submission["alert"] //"success"
$submission["content"]["answers"]["3"]["text"] //"name"

Upvotes: 2

Related Questions