PHP - 如何将两个不同的 JSON 输出合并为一个


PHP - How to merge two different JSON outputs into one

我的php服务器正在生成两个JSON输出

1.] 对于 MySql JSON 打印,我正在使用此代码。

$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.] 对于文件名打印,我正在使用此代码

chdir('./uploads');
foreach(glob('*.*') as $filename){
$data[] = $filename; 
}
echo json_encode($data);
?>

上面的两个代码都工作正常!

我希望将这两个 json 输出组合在一个页面上

不是很复杂! :)

$merged_array = array();
$merged_array[] = $data;
$merged_array[] = $values;
print json_encode($merged_array,JSON_PRETTY_PRINT);