简体   繁体   中英

How to Convert PHP Associative array to json Array

I want to convert an associative array in PHP into aa json array but the result is quite different from what i want

PHP Code

$output = array(
            "code": "UEX200",
            'email' => "username@gmail.com",
            'username' => "username,
            'type'         => "user",
            'account_manager' => "manager",
             status'       => "active
        );

i want it to be in this format

{
    "code": "UEX200",
    "details": [{
        "email": "username@gmail.com",
        "username": "username",
        "type": "user",
        "account_manager": "manager",
        "status": "active"
    }]
}

But it outputs in this format

{
    "code": "UEX200",
    "details": {
        "email": "username@gmail.com",
        "username": "username",
        "type": "user",
        "account_manager": "manager",
        "status": "active"
    }
}

Your array is not correct for the result you want change your array like mention below.

$output = array(
        "code"=> "UEX200",
        "details"=>array(
        array('email' => "username@gmail.com",
        'username' => "username",
        'type'         => "user",
        'account_manager' => "manager",
         'status'       => "active")
    )
    );

print_r(json_encode($output,true));
?>
$output = array(
        "code" => "UEX200",
      "details" => array(
        array(
        'email' => "username@gmail.com",
           'username' => "username,
           'type'         => "user",
           'account_manager' => "manager",
            status'       => "active
        )
     ); 
    );

try about code snap

{
"code": "UEX200",
"details": [{
    "email": "username@gmail.com",
    "username": "username",
    "type": "user",
    "account_manager": "manager",
    "status": "active"
}]

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM