【问题标题】:How do I add a JSON array to existing JSON in PHP?如何将 JSON 数组添加到 PHP 中的现有 JSON?
【发布时间】:2016-09-12 16:32:35
【问题描述】:

我有以下...

$json1:

[{
    "id": 1,
    "table_prefix": "movie",
    "display_name": "Movies"
}]

$json2:

[{
    "field_name": "Title",
    "column_name": "title",
    "sort_order": 0,
    "sort_section": 0,
    "required": 1,
    "relationship": "field",
    "table_type": "",
    "view_type": "text",
    "description": "",
    "multi_value": 0,
    "char_count": 255
}, {
    "field_name": "Synopsis",
    "column_name": "synopsis",
    "sort_order": 1,
    "sort_section": 0,
    "required": 0,
    "relationship": "field",
    "table_type": "",
    "view_type": "longtext",
    "description": "",
    "multi_value": 0,
    "char_count": 10000
}]

我想要$finalJSON 看起来像这样:

[{
    "id": 1,
    "table_prefix": "movie",
    "display_name": "Movies",
    "fields": [{
        "field_name": "Title",
        "column_name": "title",
        "sort_order": 0,
        "sort_section": 0,
        "required": 1,
        "relationship": "field",
        "table_type": "",
        "view_type": "text",
        "description": "",
        "multi_value": 0,
        "char_count": 255
    }, {
        "field_name": "Synopsis",
        "column_name": "synopsis",
        "sort_order": 1,
        "sort_section": 0,
        "required": 0,
        "relationship": "field",
        "table_type": "",
        "view_type": "longtext",
        "description": "",
        "multi_value": 0,
        "char_count": 10000
    }]
}]

是否有一个简单的 PHP JSON 函数可以让我像这样将 JSON 数组附加到现有的 JSON?

【问题讨论】:

  • 你真的想要一个包含 1 个对象的数组吗?
  • 这会是一件需要经常发生的事情,真的很快吗?你可以json_decode,然后随意拼接,然后用json_encode()重新编码

标签: php arrays json


【解决方案1】:

这个想法是需要使用 json_decode 将 JSON 转换为数组,操作字段,并将其编码回 JSON 格式。

试试这个:

$decoded_json = json_decode($json1, true);
$decoded_json[0]['fields'] = json_decode($json2, true);
$finalJSON = json_encode($decoded_json, JSON_PRETTY_PRINT);

【讨论】:

    【解决方案2】:

    是否有一个简单的 PHP JSON 函数允许我像这样将 JSON 数组附加到现有的 JSON?

    编程不是魔术。你能想象如果 PHP 没有数组或对象来表示你的数据吗?如果你只有字符串怎么办?除非该语言提供其他抽象方式,允许我们以其他方式表示数据,否则这将是一场噩梦。

    字符串确实可以包含歌曲或图像的字节数据。但是您会问,“是否有提高字符串音量的功能?”“我可以旋转字符串的图像内容吗?” 不。你会首先将字符串的内容解码为适当抽象的数据结构。然后,您将使用现有的函数来处理该特定域中的数据。

    不费吹灰之力就能看出 JSON 只不过是一串数据。因此,我们应该避免对数据字符串进行操作,因为其他数据结构提供了更好的操作方式。

    在这个答案中,我们将从字符串中取出数据并填充 PHP 数组和对象。因为 JSON 是如此流行的格式,PHP 提供了json_decode 来为我们做这件事。使用解码后的数据,我们可以通过简单的数组和对象属性分配将第二个数据集添加到第一个数据集。最后,使用json_encode 重新编码数据以获得您想要的结果。

    $json1 = '[{
        "id": 1,
        "table_prefix": "movie",
        "display_name": "Movies"
    }]';
    
    $json2 = '[{
        "field_name": "Title",
        "column_name": "title",
        "sort_order": 0,
        "sort_section": 0,
        "required": 1,
        "relationship": "field",
        "table_type": "",
        "view_type": "text",
        "description": "",
        "multi_value": 0,
        "char_count": 255
    }, {
        "field_name": "Synopsis",
        "column_name": "synopsis",
        "sort_order": 1,
        "sort_section": 0,
        "required": 0,
        "relationship": "field",
        "table_type": "",
        "view_type": "longtext",
        "description": "",
        "multi_value": 0,
        "char_count": 10000
    }]';
    
    $obj1 = json_decode($json1);
    $obj1[0]->fields = json_decode($json2);
    $finalJson = json_encode($obj1, JSON_PRETTY_PRINT);
    echo $finalJson;
    

    [
        {
            "id": 1,
            "table_prefix": "movie",
            "display_name": "Movies",
            "fields": [
                {
                    "field_name": "Title",
                    "column_name": "title",
                    "sort_order": 0,
                    "sort_section": 0,
                    "required": 1,
                    "relationship": "field",
                    "table_type": "",
                    "view_type": "text",
                    "description": "",
                    "multi_value": 0,
                    "char_count": 255
                },
                {
                    "field_name": "Synopsis",
                    "column_name": "synopsis",
                    "sort_order": 1,
                    "sort_section": 0,
                    "required": 0,
                    "relationship": "field",
                    "table_type": "",
                    "view_type": "longtext",
                    "description": "",
                    "multi_value": 0,
                    "char_count": 10000
                }
            ]
        }
    ]   
    

    致似乎难以解析输出的@andrew – 请查看并运行以下代码 sn-p。它解析得很好。

    console.log(JSON.parse(`[
        {
            "id": 1,
            "table_prefix": "movie",
            "display_name": "Movies",
            "fields": [
                {
                    "field_name": "Title",
                    "column_name": "title",
                    "sort_order": 0,
                    "sort_section": 0,
                    "required": 1,
                    "relationship": "field",
                    "table_type": "",
                    "view_type": "text",
                    "description": "",
                    "multi_value": 0,
                    "char_count": 255
                },
                {
                    "field_name": "Synopsis",
                    "column_name": "synopsis",
                    "sort_order": 1,
                    "sort_section": 0,
                    "required": 0,
                    "relationship": "field",
                    "table_type": "",
                    "view_type": "longtext",
                    "description": "",
                    "multi_value": 0,
                    "char_count": 10000
                }
            ]
        }
    ]`));

    【讨论】:

    • 但这是无效的 json JSON.parse('[{ "id": 1, "table_prefix": "movie", "display_name": "Movies" }]') result Uncaught SyntaxError: Invalid or unexpected token
    • @andrew 我不知道为什么你的评论出现在我的帖子上,因为那不是我在任何地方编写的代码。无论如何,我添加了一个代码 sn-p 向您展示我的 PHP 结果解析得很好。
    【解决方案3】:

    那不是 json。它是一个对象数组

    你可以简单地实现你想要的

    $json1[0]->fields = $json2;
    

    【讨论】:

    • 是json,但不清楚他是在处理字符串还是对象。
    • 我不认为这是答案
    • 由于没有引号/转义字符,我不相信这些是字符串
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多