1

I'm trying to parse data returned by the Jotform API.

I'm successfully echoing the data but I'm also getting unnecessary additional lines of text.

My PHP code is:

include "JotForm.php";

$jotformAPI = new JotForm("myapikey");

$submissions = $jotformAPI->getFormSubmissions("myformid");
var_dump($submissions );

foreach ($submissions as $data) {
    $detail = $jotformAPI->getSubmission($data['id']);

    foreach ($detail as $d) {
        echo $d[1]['answer']['first'] . '<br>';
    }
}

result of var_dump($submissions );

{
"responseCode": 200,
"message": "success",
"content": [{
    "id": "237955080346633702",
    "form_id": "31751954731962",
    "ip": "123.123.123.123",
    "created_at": "2013-06-25 03:38:00",
    "updated_at": "2013-06-27 04:58:00",
    "status": "ACTIVE",
    "new": "1",
    "answers": {
        "1": {
            "text": "Name",
            "type":"control_fullname",
            "answer": {
                "first": "LeBron",
                "last": "James"
            },
            "prettyFormat": "LeBron James"
        },
        "2": {
            "text": "Your Message",
            "type": "control_textarea",
            "answer":"¡Ay, caramba!"
        }
}],

}

And here is the result I'm getting:

1
0
0
0
C



0
LeBron
Lhen
  • 181
  • 3
  • 15
  • Please show us `var_export($detail);` At first glance, it looks like you are accessing a string @ `$d[1]['answer']` but `$d[1]['answer']['first']` might not be a string. This means php is trying to find the character (byte) that has an "offset" of `first`, but, of course, offsets must be numeric, so php converts `first` to `0` and shows you the first character in the string. ...just a hunch. Maybe the blank lines are where php is erroring out Do you have any errors in your error log? – mickmackusa Nov 02 '18 at 22:54
  • Please always include some sample input and your exact desired output from the sample data. – mickmackusa Nov 02 '18 at 23:00
  • added the var_dump result @mickmackusa. desired result is "LeBron" – Lhen Nov 02 '18 at 23:48
  • Sorry, may I see `$submissions` please? Do you only what to access the first subarray and get 1 value total? Or are you wanting to access the `['answer']['first']` value from every subarray in the data? I need to know all of the response data so that I can advice the best approach. – mickmackusa Nov 02 '18 at 23:53
  • Yes, every subarray. Suppose $submissions returns 20 entries, the hope is that it returns 20 last names. Next comment var_dump for $detail. I think I included var_dump of $detail['answer'] in the question. – Lhen Nov 03 '18 at 00:05
  • 'answers' => array (size=65) 1 => array (size=7) 'answer' => array (size=2) 'first' => string 'LeBron' (length=6) 'last' => string 'James' (length=5) 2 => .... 'answers' => array (size=65) 1 => array (size=7) 'answer' => array (size=2) 'first' => string 'Stephen' (length=7) 'last' => string 'Curry' (length=5) 2 => .... – Lhen Nov 03 '18 at 00:07

1 Answers1

1

I had to repair your invalid json string...

Code: (Demo)

$json = '{
"responseCode": 200,
"message": "success",
"content": [{
    "id": "237955080346633702",
    "form_id": "31751954731962",
    "ip": "123.123.123.123",
    "created_at": "2013-06-25 03:38:00",
    "updated_at": "2013-06-27 04:58:00",
    "status": "ACTIVE",
    "new": "1",
    "answers": {
        "1": {
            "text": "Name",
            "type":"control_fullname",
            "answer": {
                "first": "LeBron",
                "last": "James"
            },
            "prettyFormat": "LeBron James"
        },
        "2": {
            "text": "Your Message",
            "type": "control_textarea",
            "answer":"¡Ay, caramba!"
        }
}}]
}';
foreach (json_decode($json, true)['content'] as $set) {
    echo "{$set['answers'][1]['answer']['first']} {$set['answers'][1]['answer']['last']}\n";
}

Output:

LeBron James

I'm a little fuzzy on the action of the jot functions, but maybe it's supposed to be like this:

foreach ($submissions as $data) {
    $detail = $jotformAPI->getSubmission($data['id']);
    echo $detail['answers'][1]['answer']['first'] . '<br>';
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • I have to guess if this works because I don't have `$submissions` data to test with. With sample input data, I may be able to devise a more clever approach. – mickmackusa Nov 03 '18 at 00:10
  • it returned a lot of rows after the first name. Below is an example response for $jotformAPI->getSubmission($data['id']) { "responseCode": 200, "content": [{ "id": "237955080346633702", "status": "ACTIVE", "new": "1", "answers": { "3": { "text": "Name", "type":"control_fullname", "answer": { "first": "Bart", "last": "Simpson" }, "prettyFormat": "Bart Simpson" }, }], } – Lhen Nov 03 '18 at 00:14
  • I can't get started without receiving a valid json string or the `var_export($submissions)` data that I've requested. Notice that I keep saying "var_export()". or if providing a json string, don't pretty print, just echo it out. – mickmackusa Nov 03 '18 at 00:24
  • aha. the above is actually the response for $jotformAPI->getFormSubmissions("FORM ID"); as seen in their API doc: https://api.jotform.com/docs/#form-id-submissions – Lhen Nov 03 '18 at 00:26
  • When posting a question on StackOverflow, it is important that you provide the sample input and your EXACT desired output from that input. – mickmackusa Nov 03 '18 at 00:29
  • I've edited the question to include var_dump($submissions) – Lhen Nov 03 '18 at 01:01
  • your last update did it! a million thanks. saved my day. – Lhen Nov 03 '18 at 01:16