【问题标题】:How can I combine JSON objects using php?如何使用 php 组合 JSON 对象?
【发布时间】:2020-02-03 21:54:17
【问题描述】:

视图必须是这样的:

{
  "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);

【问题讨论】:

  • {} 在您的 JSON 中不平衡。你有一个未关闭的[.in 每个json。请解决此问题,以便更容易理解。
  • 尝试删除第二个$rows['app'] = array();,您正在清除之前保存的值。
  • 谢谢,现在可以使用了。你拯救了我的一天:)

标签: php mysql sql json


【解决方案1】:

因为数据库通信可能很昂贵,所以一个查询并根据需要构建结果数组可能是更有效的解决方案。

#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);

【讨论】:

  • 这是我在堆栈溢出中看到的最佳答案,我是一名想要在线配置他的应用程序的 android 开发人员,所以我是 PHP 新手,但这个答案解决了所有问题。谢谢你:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-17
  • 1970-01-01
  • 1970-01-01
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多