我有一个数组到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",
},
]
答案 0 :(得分:3)
您想要的输出表明它是一个对象数组。只需循环遍历它们并将每个子数组编码为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);
}
答案 1 :(得分:0)
你可以使用json_encode来解决这个问题吗?这会将传递的参数转换为JSON对象。
答案 2 :(得分:0)
只需使用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);
?>