通过 while 循环使用带有 group_concat 的 sql 查询,我得到了以下结果:
$rows[] = array('name' =>$row['customer'] , 'atw' =>$row['atw_number'] ,
'dr' =>array(array('drnumber'=>(($row['dr_number'])),)),);
<强>{ “名称”:“多建筑公司”, “atw”:“7146”, “博士”:[ { "博士号码": "1608,1610,1611" } ] },
相反,我可以有类似的东西吗:
<强>{ “名称”:“多建筑公司”, “atw”:“7146”, “博士”:[ { “博士号码”:“1608” }, { “博士号码”:“1610” }, { “博士号码”:“1611” } ] },
最佳答案
在 while 循环中将当前的 $rows[] = ...
替换为:
$numbers = [];
foreach (explode(',', $row['dr_number']) as $number)
$numbers[] = ['dr_number' => $number];
$rows[] = array(
'name' => $row['customer'],
'atw' => $row['atw_number'],
'dr' => $numbers
);
关于javascript - 获取 sql 结果作为嵌套 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35467520/