php - 在 PHP 中将方括号包含到单个 json 元素中
问题描述
我正在创建一个将 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"
]
]
]
]
]
];
推荐阅读
- c++ - 检查数组的相邻值
- javascript - Uncaught (in promise) TypeError: $(...).ready(...).then is not a function
- x86 - x86 对不可缓存内存的锁定前缀是否会导致内存带宽拒绝服务?
- vector - 怀疑2个无符号向量的总和及其输出(他的向量长度)VHDL
- javascript - Angularjs:更改嵌套 ng-repeat 特定索引的对象属性
- javascript - 无法解决此错误:无法读取 JSON:由于输入结束,没有要映射的内容
- python-3.x - 如何使用最大数组
- java - JavaFX setCellValueFactory
- go - Go Protobuf 精确小数
- python-3.x - Pandas 数据框中的新列与给定列中的重复项有关