【问题标题】:How to convert multidimensional array into json object [closed]如何将多维数组转换为json对象[关闭]
【发布时间】: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 对象中有一些重复!还是只是一个错字!!
  • 不不,它只是一个拼写错误,每个键都有不同的值。 @someOne

标签: php arrays json multidimensional-array


【解决方案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);

?>

【讨论】:

    猜你喜欢
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 2016-11-28
    相关资源
    最近更新 更多