Answered

Empty fields not Showing in Array


Badge +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

icon

Best answer by mathio 24 November 2022, 10:56

View original

15 replies

Userlevel 7
Badge +5

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

Badge +1

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

Thanks

 

Userlevel 7
Badge +5

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.

Badge +1

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?? 

 

Userlevel 7
Badge +5

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

Badge +1

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 

Userlevel 7
Badge +5

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.)

Badge +1

i will try 

 

Badge +1

Hai again.. Its not working

 

Userlevel 7
Badge +5

I am afraid you will need to be more specific.

Badge +1

[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.

 

Userlevel 7
Badge +5

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

 

Badge +1

i will try this and will let you know thanks

 

Badge +1

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

 

 

Badge +1

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

 

 

i cannot fetch all deatils in the array 

Reply