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
Answered
Empty fields not Showing in Array
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
Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.