我正在尝试从mysql获取正确的Json数据。我取得了很大的进步。看一看;
我有如下表:
name folder path
RayMala 787 01.jpg,02.jpg,03.jpg,04.jpg,05.jpg...
RayMala 788 01.jpg,02.jpg,03.jpg,04.jpg,05.jpg,06.jpg...
Falitiko 332 01.jpg...
Falitiko 333 01.jpg,02.jpg...
我的当前代码:从单个表格单元格制作数组。
$rows = array();
while($r = mysqli_fetch_assoc($result)) {
$rows[] = $r;
}
$nArray = array();
foreach($rows as $value){
$nArray[] = array('name' => $value['name'], 'folder' => $value['folder'], 'path' => explode(",", $value['path']));
}
print json_encode($nArray);
当前JSON输出:请参阅名称值重复。
[
{
"name": "RayMala",
"folder": "787",
"Paths": ["1.jpg", "2.jpg", "3.jpg"]
},
{
"name": "RayMala",
"folder": "788",
"Paths": ["1.jpg", "2.jpg", "3.jpg"]
},
{
"name": "Falitiko",
"folder": "333",
"Paths": ["1.jpg", "2.jpg", "3.jpg"]
}
]
通缉JSON输出:
[
{
"name":"RayMala",
"random": [
{
"folder": "787",
"Paths": ["1.jpg", "2.jpg", "3.jpg"]
},
{
"folder": "788",
"Paths": ["1.jpg", "2.jpg", "3.jpg"]
}
]
},
{
"name":"Falitiko",
"random": [
{
"folder": "332",
"Paths": ["1.jpg", "2.jpg", "3.jpg"]
},
{
"folder": "333",
"Paths": ["1.jpg", "2.jpg", "3.jpg"]
}
]
}
]
答案 0 :(得分:2)
$nArray = array();
while($r = mysqli_fetch_assoc($result)) {
$nArray[$r["name"]][] = array('folder' => $r['folder'], 'path' => explode(",", $r['path']));
}
$output_arr = array();
foreach($nArray as $key=>$value){
$output_arr[]=array("name"=>$key,"random"=>$value);
}
echo json_encode($output_arr);