如何在PHP中将现有JSON添加JSON数组?

时间:2016-09-12 16:32:36

标签: php arrays json

我有以下......

$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?

3 个答案:

答案 0 :(得分:3)

  

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

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

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

观察JSON只不过是一串数据需要花费很少的精力。因此,我们应该避免对数据字符串进行操作,因为其他数据结构提供了更好的操作方法。

在这个答案中,我们将从字符串中取出数据并填充PHP Arrays和Objects。因为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 - 请查看并运行以下代码段。它解析得很好。



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
            }
        ]
    }
]`));




答案 1 :(得分:1)

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

只需使用

即可实现您想要的效果
$json1[0]->fields = $json2;

答案 2 :(得分: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);