我试图为 Rubrics 生成特定的 json,仅需要以下格式。为什么我需要这个? 对于 SurveyJS,我正在使用 PHP 创建单元格 jsonPHP 输出Array( [Attendance] => Array ( [0] => Array ( [OUTSTANDING] => A ) [1] => Array ( [Satisfactory] => B ) [2] => Array ( [Needs Improvement] => C ) [3] => Array ( [Unacceptable] => D ) ) [Punctuality] => Array ( [0] => Array ( [OUTSTANDING] => A ) [1] => Array ( [Satisfactory] => B ) [2] => Array ( [Needs Improvement] => C ) [3] => Array ( [Unacceptable] => D ) ))我一直在努力实现和要求的输出 { "Attendance": { "OUTSTANDING": "A", "Satisfactory": "B", "Needs Improvement": "C", "Unacceptable": "D" }, "Punctuality": { "OUTSTANDING": "A", "Satisfactory": "B", "Needs Improvement": "C", "Unacceptable": "D" } }
2 回答
慕斯王
TA贡献1864条经验 获得超2个赞
您需要展平每个子数组。鉴于该结构,这应该有效:
foreach($array as $key => $val) { $array[$key] = array_merge(...$val); }
如果您有旧的 PHP 版本,那么:
$array[$key] = call_user_func_array('array_merge', $val);
郎朗坤
TA贡献1921条经验 获得超9个赞
根据json.org规范,关联数组将转换为对象,而平面数组将转换为数组,因此您需要类似以下内容:
<?php
$arr = [
'Attendance' => [
'OUTSTANDING' => 'A',
'Satisfactory' => 'B',
'Needs Improvement' => 'C',
'Unacceptable' => 'D'
],
'Punctuality' => [
'OUTSTANDING' => 'A',
'Satisfactory' => 'B',
'Needs Improvement' => 'C',
'Unacceptable' => 'D'
]
];
echo '<pre>';
echo json_encode($arr, JSON_PRETTY_PRINT);
- 2 回答
- 0 关注
- 123 浏览
添加回答
举报
0/150
提交
取消