如何使用php组合JSON对象?
视图必须如下所示:如何使用php组合JSON对象?,php,mysql,sql,json,Php,Mysql,Sql,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", "sp
{
"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
}
}
但当我尝试这样做时,它会覆盖旧函数,并像这样打印最后一个对象:
{
"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);
因为数据库通信可能很昂贵,所以一次查询并根据需要构建结果数组可能是更有效的解决方案
#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);
{}
在JSON中不平衡。您在每个json中都有一个unclose[
。请修复此问题,以便更容易理解。尝试删除第二个$rows['app']=array();
,您正在清除以前保存的值。谢谢,它现在起作用了。您保存了我的一天:)json中的{/code>不平衡。您有一个unclose[
。在每个json中。请修复此问题,以便更容易理解。请尝试删除第二个$rows['app']=array();
,您正在清除以前保存的值。谢谢,它现在起作用了。您保存了我的一天:)这是我在stack overflow上看到的最好的答案,我是一个android开发人员,希望在线配置他的应用程序,所以,我对PHP是新手,但这个答案解决了所有问题。谢谢:)这是我在stack overflow上看到的最好答案,我是一个android开发人员,不想这样做o在线配置他的应用程序,所以我对PHP不熟悉,但这个答案解决了所有问题。谢谢:)