@mathio or @picsoung may be able to help with this!
Liz wrote:
@mathio or @picsoung may be able to help with this!
Thanks
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.
hi again.
>answers] => Array
(
0] => stdClass Object
(
type] => text
text] => Akhil
field] => stdClass Object
(
id] => jtvihHvVO9s2
οΏ½type] => short_text
[ )]
)
this is my answers array so im using $name = answersB0]->text; to store values .. How can i store using id like answers/0]->text-> id] this??
You should be able to access the ID using answerss0]->field->id
Ok. But i have one doubt
>8] => stdClass Object
(
οΏ½type] => phone_number
οΏ½phone_number] => +00222333445
2field] => 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 o7] .. Hope u understand
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 = $idsi $answer->field->id ];
$dataa $fieldName ] = $answer->text;
}
var_dump( $data );
(The code above is not tested.)
Hai again.. Its not working
I am afraid you will need to be more specific.
[answers] => Array
(
οΏ½0] => stdClass Object
(
/type] => text
=text] => Akhil
οΏ½field] => stdClass Object
(
οΏ½id] => jtvihHvVO9s2
οΏ½type] => short_text
bref] => 01G947C83M5HBFDKK58BPBCDBA
)
)
οΏ½field] => stdClass Object
(
)id] => jtvihHvVO9s2
ltype] => 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 Btext]=>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->answersn0]->text;
$description = $detail->answersb9]->file_url;
}
THIS Is how i point those filed values into variables.
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 ];
$fieldse $fieldName ] = $answer->text;
}
var_dump(fields);
i will try this and will let you know thanks
Should i collect all ids and store it into $ids array ?? static?
akhilser wrote:
Should i collect all ids and store it into $ids array ?? static?
i cannot fetch all deatils in the array