如何将多维数组转换为json对象



我在数组到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字符串,然后再次解码以获得一个对象:

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);
}

您可以使用json_encode来解决这个问题吗?这将把传递的参数转换为JSON对象。

只需使用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);
?>

相关内容

  • 没有找到相关文章

最新更新