2 回答
TA贡献1802条经验 获得超4个赞
您可以通过将所有数组元素转换为之前的对象来制作表示对象数组的 JSON 字符串json_encode。然而,这将给出字符串键,而不是数字键(这不是有效的 JSON):
$array = [
0 => [0 => "Value1", 1 => "Value2", 2 => "Value3" ],
1 => [0 => "Test", 1 => "Test2", 2 => "Test3" ],
2 => [0 => "Random1", 1 => "Random2", 2 => "Random3" ]
];
foreach ($array as &$arr) {
$arr = (object)$arr;
}
echo json_encode($array, JSON_PRETTY_PRINT);
输出:
[
{
"0": "Value1",
"1": "Value2",
"2": "Value3"
},
{
"0": "Test",
"1": "Test2",
"2": "Test3"
},
{
"0": "Random1",
"1": "Random2",
"2": "Random3"
}
]
更新
根据对问题的编辑,您似乎正在寻找这个:
$array = [
[
["item1"],
["itemname"],
["item desc"],
["05/14/2014"]
],
[
["item12"],
["itemname2"],
["item desc2"],
["05/14/2014"]
]
];
foreach ($array as &$arr) {
$arr = (object)array_map(function ($a) { return $a[0]; }, $arr);
}
echo json_encode($array, JSON_PRETTY_PRINT);
输出:
[
{
"0": "item1",
"1": "itemname",
"2": "item desc",
"3": "05\/14\/2014"
},
{
"0": "item12",
"1": "itemname2",
"2": "item desc2",
"3": "05\/14\/2014"
}
]
- 2 回答
- 0 关注
- 128 浏览
添加回答
举报