PHP - 如何将两个不同的JSON输出合并为一个
问题描述:
My php server is generating two JSON output
1.] For MySql JSON printing I am using this code.
$sql = "select id ,Title , Meassage from lodhinews";
$result = $conn->query($sql);
$values = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$values['data'][] = array(
'id'=>$row['id'],
'Title'=>$row['Title'],
'Meassage'=>$row['Meassage']
);
}
header('Content-Type: application/json;charset=utf-8');
echo json_encode($values ,JSON_PRETTY_PRINT);
} else {
$values = array(
'error'=>'No results found'
);
}
$conn->close();
?>
2.] For file name printing I am using this code
chdir('./uploads');
foreach(glob('*.*') as $filename){
$data[] = $filename;
}
echo json_encode($data);
?>
both the above code is working fine!
I wanted this both json output combined on one single page
我的php服务器正在生成两个JSON输出 h2>
1。]对于MySql JSON打印我正在使用此代码。 p>
$ sql =“select id,Title,Meassage from lodhinews”;
$ result = $ conn-> query($ sql );
$ values = array();
if($ result-> num_rows> 0){
//输出每行的数据
while($ row = $ result-> fetch_assoc()){
$ values ['data'] [] = array(
'id'=> $ row ['id'],
'Title'=> $ row ['Title'],
'Meassage '=> $ row ['Meassage']
);
}
header('Content-Type:application / json; charset = utf-8');
echo json_encode($ values,JSON_PRETTY_PRINT);
} else {
$ values = array(
'error'=>'找不到结果'
);
}
$ conn-> close();
?>
code> pre>
2。]对于文件名打印,我使用此代码
p>
chdir('./ uploads');
foreach(glob('*。*')as $ filename){
$ data [] = $ filename;
}
echo json_encode($ data);
?>
code> pre>
上述代码都运行正常! p>
我希望这两个json输出组合在一个页面上 strong> p>
div>
答
not very complicated ! :)
$merged_array = array();
$merged_array[] = $data;
$merged_array[] = $values;
print json_encode($merged_array,JSON_PRETTY_PRINT);