我从数据库中获取信息。比如我的数据库数据如下: 我从数据库中获取信息。比如我的数据库数据如下:
-------------------------------------------------
p_id | description_name | description_value |
-------------------------------------------------
1 | author | X1 |
-------------------------------------------------
1 | editor | X2 |
-------------------------------------------------
2 | author | Y1 |
-------------------------------------------------
3 | author | Z1 |
-------------------------------------------------
3 | editor | Z2 |
------------------------------------------------- ...
所以在 php 代码部分我使用这种方法来获取descriptions
:
<?php
...
$result5 = mysqli_query($conn,"SELECT * FROM `descriptions` ");
while ($row5 = $result5 ->fetch_assoc()){
$des[] = [(int) $row5['p_id'] ,[ $row5['description_name'] => $row5['description_value']] ] ;
}
echo json_encode( $des);
?>
这是这段代码的输出:
[[1,{"author": "X1"}],[1,{"editor": "X2"}],[2,{"author": "Y1"}],[3,{"author": "Z1"}],[3,{"editor": "Z2"}],[ ...
但是,我的预期输出是这样的:
[{"p_id" : 1,"author": "X1","editor": "X2"},{"p_id" : 2, "author": "Y1"},{"p_id" : 3,"author": "Z1","editor": "Z2"},{...
我希望在您的指导下,这个问题将为我解决,谢谢...