将PHP数组转换为Json包含对象

时间:2017-04-17 06:05:32

标签: php arrays json

我需要使用json_encode()

从php数组创建以下json
{
"Image":"wordpress",
"Env" :[
    "VIRTUAL_HOST=domain.com"
    ],
"ExposedPorts":{
    "8080":"80"
    }
}

我被困在ExposedPorts,请帮忙。这是我的php数组,但它不起作用:

[
     'Image'=> 'wordpress',
      'Env' => [
            "VIRTUAL_HOST=domain.com"
               ],

     'ExposedPorts'=>json_encode(["8080"=>"80"],JSON_FORCE_OBJECT)
]

1 个答案:

答案 0 :(得分:1)

为什么不像这样json_encode整个数组,

<?php
$array = array(
"Image" =>"wordpress",
"Env" =>
    ["VIRTUAL_HOST=domain.com"
    ],
"ExposedPorts"=>
    array("8080"=>"80"));

    echo json_encode($array);

输出:

{"Image":"wordpress","Env":["VIRTUAL_HOST=domain.com"],"ExposedPorts":{"8080":"80"}}