php - 在 PHP 中获取第二级 JSON 数据

标签 php arrays 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>';
}

关于php - 在 PHP 中获取第二级 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37497148/

相关文章:

php - 在我的核心 PHP 站点或 CodeIgniter 站点上显示 Woocommerce 产品和 Magento 产品

php - sql中如何查找一列中有多少个不同的单词?

php - 建议 : 8 million send request

arrays - 将 SearchBar 添加到 uitableview 在过滤过程中遇到错误

javascript - 如何循环遍历数组(递归)并将对象键/值对推送到另一个数组中?

javascript - 如何使用 For 循环反向打印字符串?

java - HTTP/1.1 415 处理 JSON 时不支持的媒体类型

php - 如果存在mysql错误

c# - 如何将 JSON 数组转换为 C# 列表?

json - 使用 AZ CLI 提取应用服务计划 Azure 资源列表