Empty fields not Showing in Array | Community
Skip to main content
Answered

Empty fields not Showing in Array


akhilser
Forum|alt.badge.img+1

Im fetching typeform data throgh Webhooks into my Wordpress website i retrieve the data as a json and converted to array formats .. The problem is if i have 25 fields i can access the fields using array index example array[0]=>1st field , [1]=> 2nd field .. If the [1] is enmpty the [2] index field will be become [1] so u cannot point index values to my backend custom fields as corresponding field values.. If i set those fields as required its working but the fields are optional so i need a solution for this issue … THanks

Best answer by mathio-tf

I think you are doing foreach on incorrect variable. You should do foreach on $data->form_response->answers.

My approach would be to dynamically build an array of $fields and then process the response from there. And not refer to any hard-coded index in answers array. Like this:

$data = json_decode(file_get_contents('php://input'), true); 
$answers = $data->form_response->answers;

$fields = array();
$ids = array(
  'LrykzSE8QBEQ' => 'rating',
  'XIshbqxMr7n2' => 'phone',
  'GPHAADhlVLgy' => 'true-false',
  // ...
);

foreach($answers as $answer){
  $fieldName = $ids[ $answer->field->id ];

  // Note: answer is not always in "text", it depends on question type
  $fields[ $fieldName ] = $answer->text;
}

var_dump(fields); // this is where my data is now, with custom keys

 

View original

15 replies

Liz
Community Team
Forum|alt.badge.img+5
  • Tech Community Advocate
  • 14905 replies
  • November 22, 2022

@mathio or @picsoung may be able to help with this!


akhilser
Forum|alt.badge.img+1
  • Author
  • Explorer
  • 11 replies
  • November 23, 2022
Liz wrote:

@mathio or @picsoung may be able to help with this!

Thanks

 


mathio-tf
Typeform
Forum|alt.badge.img+5
  • Typeform
  • 888 replies
  • November 23, 2022

Hello @akhilser and @Liz 

when you receive the payload with answers you can notice that each answer object has the following structure (taken from our docs):

{
    "type": "email",
    "email": "laura@example.com",
    "field": {
        "id": "SMEUb7VJz92Q",
        "type": "email"
    }
},

Each answer is uniquely identified with field.id to indicate which question it belongs to. This field.id never changes and each question will always have the same value.

You should not rely on indexes, because as you said some answers can be omitted or answers might even be in different order based on form logic.


akhilser
Forum|alt.badge.img+1
  • Author
  • Explorer
  • 11 replies
  • November 23, 2022

hi again. 

[answers] => Array
                (
                    [0] => stdClass Object
                        (
                            [type] => text
                            [text] => Akhil
                            [field] => stdClass Object
                                (
                                    [id] => jtvihHvVO9s2
                                    [type] => short_text
                                    [ref] => 01G947C83M5HBFDKK58BPBCDBA
                                )

                        )
this is my answers array so im using $name = answers[0]->text; to store values .. How can i store using id like answers[0]->text->[id] this?? 

 


mathio-tf
Typeform
Forum|alt.badge.img+5
  • Typeform
  • 888 replies
  • November 23, 2022

You should be able to access the ID using answers[0]->field->id 


akhilser
Forum|alt.badge.img+1
  • Author
  • Explorer
  • 11 replies
  • November 23, 2022

Ok. But i have one doubt 

[8] => stdClass Object
                        (
                            [type] => phone_number
                            [phone_number] => +00222333445
                            [field] => stdClass Object
                                (
                                    [id] => tu9WDKbicj6c
                                    [type] => phone_number
                                    [ref] => 546378f5-85b0-4bd3-8d92-0e1f8d486adf
                                )

                        )
if the [7]th field is empty then the above array is become 7 then how can i access with the index like if i call with index 8 like answers[8]->field->id->phone_number . But the data i want is become [7] .. Hope u understand 


mathio-tf
Typeform
Forum|alt.badge.img+5
  • Typeform
  • 888 replies
  • November 23, 2022

You should process the answers array in a loop. Maybe build an associative array? 

Something like this:

$data = array();
$ids = array(
  'foo' => 'email',
  'bar' => 'name',
  // ...
);

foreach ($answers as $answer) {
  $fieldName = $ids[ $answer->field->id ];
  $data[ $fieldName ] = $answer->text;
}

var_dump( $data );
// array( 'email' => 'john@example.com', 'name' => 'John Doe', ... )

(The code above is not tested.)


akhilser
Forum|alt.badge.img+1
  • Author
  • Explorer
  • 11 replies
  • November 23, 2022

i will try 

 


akhilser
Forum|alt.badge.img+1
  • Author
  • Explorer
  • 11 replies
  • November 24, 2022

Hai again.. Its not working

 


mathio-tf
Typeform
Forum|alt.badge.img+5
  • Typeform
  • 888 replies
  • November 24, 2022

I am afraid you will need to be more specific.


akhilser
Forum|alt.badge.img+1
  • Author
  • Explorer
  • 11 replies
  • November 24, 2022

[answers] => Array
                (
                    [0] => stdClass Object
                        (
                            [type] => text
                            [text] => Akhil
                            [field] => stdClass Object
                                (
                                    [id] => jtvihHvVO9s2
                                    [type] => short_text
                                    [ref] => 01G947C83M5HBFDKK58BPBCDBA
                                )

                        )

 

[field] => stdClass Object
                                (
                                    [id] => jtvihHvVO9s2
                                    [type] => short_text
                                    [ref] => 01G947C83M5HBFDKK58BPBCDBA
                                )
this array is inside the [0] array . So the form will omit the empty fileds but the id of the field is unique its will not change like you said but i cannot access that id and i cant fetch [text]=>Akhil using [id]=>jtvihHvVO9s2

$data = json_decode(file_get_contents('php://input'),true); 
using this i can fetch typeform form data .. 

foreach($data as $detail){
        $name = $detail->answers[0]->text;
        $description = $detail->answers[9]->file_url;

}

THIS Is how i point those filed values into variables.

 


mathio-tf
Typeform
Forum|alt.badge.img+5
  • Typeform
  • 888 replies
  • Answer
  • November 24, 2022

I think you are doing foreach on incorrect variable. You should do foreach on $data->form_response->answers.

My approach would be to dynamically build an array of $fields and then process the response from there. And not refer to any hard-coded index in answers array. Like this:

$data = json_decode(file_get_contents('php://input'), true); 
$answers = $data->form_response->answers;

$fields = array();
$ids = array(
  'LrykzSE8QBEQ' => 'rating',
  'XIshbqxMr7n2' => 'phone',
  'GPHAADhlVLgy' => 'true-false',
  // ...
);

foreach($answers as $answer){
  $fieldName = $ids[ $answer->field->id ];

  // Note: answer is not always in "text", it depends on question type
  $fields[ $fieldName ] = $answer->text;
}

var_dump(fields); // this is where my data is now, with custom keys

 


akhilser
Forum|alt.badge.img+1
  • Author
  • Explorer
  • 11 replies
  • November 24, 2022

i will try this and will let you know thanks

 


akhilser
Forum|alt.badge.img+1
  • Author
  • Explorer
  • 11 replies
  • November 24, 2022

Should i collect all ids and store it into $ids array ?? static?

 

 


akhilser
Forum|alt.badge.img+1
  • Author
  • Explorer
  • 11 replies
  • November 25, 2022
akhilser wrote:

Should i collect all ids and store it into $ids array ?? static?

 

 

i cannot fetch all deatils in the array 


Reply