How to convert multidimensional array to json object

I have a problem with an array to convert json, I have an array and I want to convert this array to json objects, the desired result is below, so any of them will help me.

Php scale

    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] => 



        )

)

      

Convert above array to json object like this:

[
        {
            "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",

        },

]  

      

+3


source to share


3 answers


Your desired output assumes it is an array of objects. Just loop over them and encode each subarray into a json string and decode it again to get an object:

foreach($array as $k =>$a){
    $array[$k] = json_decode(json_encode($a));
}

      



If you mean you want an array of json strings, omit json_decode

:

foreach($array as $k =>$a){
    $array[$k] = json_encode($a);
}

      

+3


source


Can you just use json_encode for this problem? This will convert the passed argument to a JSON object.



0


source


Just use 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);

?>

      

0


source







All Articles