如何使用 php 组合 JSON 对象?



视图必须如下所示:

{
"app": [
{
"system": {
"base_url": "https://youtube.com",
"email_address": "facebook@gmail.com",
"privacy_policy_url": "https://googles.com",
"terms_of_use_url": "https://fasfg.com",
"splash_screen_timout": 1000,
"loading_spinner": "Wave"
}
},
{
"settings": {
"dark_mode": true,
"screen_sleep": true,
"fullscreen_mode": true,
"notification_sound_option": true,
"notification_vibration_option": true,
"download_via_wifi": true,
"device_information": true,
"other_links": true,
"danger_zone": true
}
}

但是当我尝试这样做时,它会覆盖旧函数并像这样打印最后一个对象:

{
"app": [
{
"settings": {
"dark_mode": true,
"screen_sleep": true,
"fullscreen_mode": true,
"notification_sound_option": true,
"notification_vibration_option": true,
"download_via_wifi": true,
"device_information": true,
"other_links": true,
"danger_zone": true
}
}

这是我的PHP代码,我知道问题所在,但我无法解决它,我认为答案是附加数据,但我不知道如何,我搜索了答案,但对我来说没有什么清楚的:

$fetch_system = $db->prepare("SELECT base_url, email_address, 
privacy_policy_url, terms_of_use_url, 
splash_screen_timout, loading_spinner 
FROM configuration 
WHERE secret_api_key=?");
$fetch_system->bind_param("s", $_SESSION['secret_api_key']);
$fetch_system->execute();
$rows = array();
$result = $fetch_system->get_result();
while($rows1 = $result->fetch_assoc()) {
$rows['app'] = $rows1;
}

$fetch_settings = $db->prepare("SELECT dark_mode, screen_sleep, full_screen, 
notification_sound, notification_vibration, 
download_via_wifi, device_information, 
other_links, danger_zone 
FROM configuration  
WHERE secret_api_key=?");
$fetch_settings->bind_param("s", $_SESSION['secret_api_key']);
$fetch_settings->execute();
$rows['app'] = array();
$result = $fetch_settings->get_result();
while($rows2 = $result->fetch_assoc()) {
$rows['settings'] = $rows2;
}
echo json_encode($rows);

由于数据库通信可能很昂贵,因此根据需要进行一次查询并构建生成的数组可能是更有效的解决方案。

#get all configuration by secret_api_key
$fetch_system = $db->prepare("SELECT * FROM configuration WHERE secret_api_key=?");
$fetch_system->bind_param("s", $_SESSION['secret_api_key']);
$fetch_system->execute();
$result = $fetch_system->get_result();
$configuration = $result->fetch_assoc();
#build result from data
$rows = array(
'app'=>array(
'system' => array (
'base_url' => $configuration['base_url'],
'email_address' => $configuration['email_address'],
'privacy_policy_url' => $configuration['privacy_policy_url'],
'terms_of_use_url' => $configuration['terms_of_use_url'],
'splash_screen_timout' => $configuration['splash_screen_timout'],
'loading_spinner' => $configuration['loading_spinner']
),
'settings' => array (
'dark_mode' => $configuration['dark_mode'],
'screen_sleep' => $configuration['screen_sleep'],
'fullscreen_mode' => $configuration['fullscreen_mode'],
'notification_sound_option' => $configuration['notification_sound_option'],
'notification_vibration_option' => $configuration['notification_vibration_option'],
'download_via_wifi' => $configuration['download_via_wifi'],
'device_information' => $configuration['device_information'],
'other_links' => $configuration['other_links'],
'danger_zone' => $configuration['danger_zone']
)
)
);
echo json_encode($rows);

最新更新