我有遵循此模型的JSON数据:
{
"questions": [
{
"questionKey": 0,
"question": "string",
"required": true,
"type": "multipleChoice",
"maxSize": 0,
"answers": [
{
"answer": "string",
"answerKey": 0
}
]
}
],
"fields": [
{
"field": "string",
"answers": [
"string"
],
"required": true,
"maxSize": 0
}
]
}
我很难获得所有字段>字段值。当我执行var_dump($ jsondata)时,它会打印所有数据,因此我知道我正在获取并存储数据。我知道我需要做一个foreach语句,如:
foreach ($jsondata as $data) {
echo $data['fields'];
}
我知道这是错的,但我不知道如何循环遍历所有字段>字段值。非常感谢您的帮助:)
答案 0 :(得分:4)
尝试这样的事情:
//Decode JSON string into a PHP array.
$jsonData = json_decode($jsonStr, true);
//Loop through it like any associative array.
foreach($jsonData['fields'] as $field){
var_dump($field);
echo $field['field'] , '<br>';
}