我有如下表:
folder Paths
788 "01.jpg","02.jpg","03.jpg","04.jpg","05.jpg","06.jpg..........
332 "01.jpg"...................
333 "01.jpg","02.jpg"...........
想要JSON输出如下:
{
"folder": "788",
"Paths": ["1.jpg", "2.jpg", "3.jpg"]
},
{
"folder": "333",
"Paths": ["1.jpg", "2.jpg", "3.jpg"]
}
这可能吗?
答案 0 :(得分:1)
您可以根据需要解析它,在您的方案中,{{3}}会让您的工作更轻松,请参阅下面的用法: -
$nArray = array();
foreach($arr as $value){
$nArray[] = array('folder' => $value['folder'], 'Paths' => explode(",", $value['Paths']));
}
但是,如果可能的话,我建议你改变你的数据库结构,并在单独的表中取出paths
列,这样会有两个表,
答案 1 :(得分:0)
将该表放入数组中,您的表将类似于$ array并执行此操作。
<?php
$array = array(array('folder' => '788', 'Paths' => '"01.jpg", "02.jpg", "03.jpg", "04.jpg", "05.jpg"'),array('folder'=>'332','Paths'=>'01.jpg'));
$output = "";
foreach ($array as $val) {
if (is_array($val)) {
$output[] = array('folder' => $val['folder'], 'Paths' => explode(",",$val['Paths']));
}
}
if (is_array($output)) {
$output = json_encode($output);
}
echo $output;
?>