gpt4 book ai didi

php - 如何将 JSON 数组添加到 PHP 中的现有 JSON 中?

转载 作者:行者123 更新时间:2023-12-02 07:38:55 25 4
gpt4 key购买 nike

我有以下内容...

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

最佳答案

这个想法是需要使用 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);

关于php - 如何将 JSON 数组添加到 PHP 中的现有 JSON 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39454885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com