如何将多维数组转换为json对象[关闭]
Posted
技术标签:
【中文标题】如何将多维数组转换为json对象[关闭]【英文标题】:How to convert multidimensional array into json object [closed] 【发布时间】:2015-09-19 14:57:05 【问题描述】:我在数组到 json 的转换中遇到问题,我有一个数组,我想将此数组转换为 json 对象,下面给出了所需的输出,所以请任何人帮助我。
php 数组
Array
(
[0] => Array
(
[application_id] => 132
[application_status] => SUBMITTED
[reference_number] =>
[salutation] =>
[first_name] =>
[middle_name] =>
[last_name] =>
[mother_name] =>
)
[1] => Array
(
[application_id] => 148
[application_status] => SUBMITTED
[reference_number] =>
[salutation] =>
[first_name] =>
[middle_name] =>
[last_name] =>
[mother_name] =>
)
[2] => Array
(
[application_id] => 154
[application_status] => SUBMITTED
[reference_number] =>
[salutation] =>
[first_name] =>
[middle_name] =>
[last_name] =>
[mother_name] =>
)
[3] => Array
(
[application_id] => 182
[application_status] => SUBMITTED
[reference_number] =>
[salutation] =>
[first_name] =>
[middle_name] =>
[last_name] =>
[mother_name] =>
)
[4] => Array
(
[application_id] => 186
[application_status] => SUBMITTED
[reference_number] =>
[salutation] =>
[first_name] =>
[middle_name] =>
[last_name] =>
[mother_name] =>
)
)
像这样将上面的数组转换为 json 对象:
[
"application_id": "1",
"application_status": "0",
"reference_number": "/index",
"salutation": "index",
"first_name": "Index",
"middle_name": "Home",
"last_name": "1",
,
"application_id": "1",
"application_status": "0",
"reference_number": "/index",
"salutation": "index",
"first_name": "Index",
"middle_name": "Home",
"last_name": "1",
,
"application_id": "1",
"application_status": "0",
"reference_number": "/index",
"salutation": "index",
"first_name": "Index",
"middle_name": "Home",
"last_name": "1",
,
"application_id": "1",
"application_status": "0",
"reference_number": "/index",
"salutation": "index",
"first_name": "Index",
"middle_name": "Home",
"last_name": "1",
,
"application_id": "1",
"application_status": "0",
"reference_number": "/index",
"salutation": "index",
"first_name": "Index",
"middle_name": "Home",
"last_name": "1",
,
]
【问题讨论】:
json_encode
?
你真的想在一个不知从何而来的 JSON 对象中有一些重复!还是只是一个错字!!
不不,它只是一个拼写错误,每个键都有不同的值。 @someOne
【参考方案1】:
您想要的输出表明它是一个对象数组。只需遍历它们并将每个子数组编码为 json 字符串,然后再次解码以获得一个对象:
foreach($array as $k =>$a)
$array[$k] = json_decode(json_encode($a));
如果您的意思是想要一个 json 字符串数组,请省略 json_decode
:
foreach($array as $k =>$a)
$array[$k] = json_encode($a);
【讨论】:
这对我有用,谢谢。【参考方案2】:您可以只使用json_encode 来解决这个问题吗?这会将传递的参数转换为 JSON 对象。
【讨论】:
没有 json_encode 给出这样的数据: "0": "application_id": "132", "application_status": "SUBMITTED", "reference_number": null, "salutation": null, "1": "application_id": "134", "application_status": "SUBMITTED", "reference_number": null, "salutation": null, 【参考方案3】:只需使用 json_encode()
<?php
$array = Array
(
"0" => Array
(
"application_id" => "132",
"application_status" => "SUBMITTED",
"reference_number" => "",
"salutation" => "",
"first_name" => "",
"middle_name" => "",
"last_name" => "",
"mother_name" => ""
),
"1" => Array
(
"application_id" => "148",
"application_status" => "SUBMITTED",
"reference_number" => "",
"salutation" => "",
"first_name" => "",
"middle_name" => "",
"last_name" => "",
"mother_name" => ""
),
"2" => Array
(
"application_id" => "154",
"application_status" => "SUBMITTED",
"reference_number" => "",
"salutation" => "",
"first_name" => "",
"middle_name" => "",
"last_name" => "",
"mother_name" => ""
),
"3" => Array
(
"application_id" => "182",
"application_status" => "SUBMITTED",
"reference_number" => "",
"salutation" => "",
"first_name" => "",
"middle_name" => "",
"last_name" => "",
"mother_name" => ""
),
"4" => Array
(
"application_id" => "186",
"application_status" => "SUBMITTED",
"reference_number" => "",
"salutation" => "",
"first_name" => "",
"middle_name" => "",
"last_name" => "",
"mother_name" => ""
)
);
$json = json_encode($array);
print_r($json);
?>
【讨论】:
以上是关于如何将多维数组转换为json对象[关闭]的主要内容,如果未能解决你的问题,请参考以下文章