我正在创建一个函数来将JSON导出为指定格式。我需要在单个元素中包含方括号。如您所见,"details_order"包含一个带有大括号的元素,我所需要的只是在其中添加一个额外的方括号。请指教,谢谢
原始输出:
[
{
"reference": "20190531",
"orders": [
{
"id": "12345",
"label": "22776",
"address": "ABC, Apple road",
"details_orders": {
"ref": "AB07-332C"
}
}
]
}
]
预期输出:
[
{
"reference": "20190531",
"orders": [
{
"id": "12345",
"label": "22776",
"address": "ABC, Apple road",
"details_orders": [
{
"ref": "AB07-332C"
}
]
}
]
}
]
将数据结构编码为 JSON 格式时,只需确保"ref"值存储在"details_orders"数组内的数组中即可。
下面是如何在 PHP 中构建数据以输出所需 JSON 的示例:
Array
(
[0] => Array
(
[reference] => 20190531
[orders] => Array
(
[0] => Array
(
[id] => 12345
[label] => 22776
[address] => ABC, Apple road
[details_orders] => Array
(
[0] => Array
(
[ref] => AB07-332C
)
)
)
)
)
)
以下是在 PHP 中初始化此类数组的方法:
$data = [
[
"reference" => "20190531",
"orders" => [
[
"id" => "12345",
"label" => "22776",
"address" => "ABC, Apple road",
"details_orders" => [
[
"ref" => "AB07-332C"
]
]
]
]
]
];