在 PHP 中获取第二级 JSON 数据



我有遵循此模型的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'];
}

我知道这是错误的,但我不知道如何循环遍历所有字段>字段值。非常感谢您的帮助:)

尝试这样的事情:

//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>';
}

最新更新