2

I need to create following json from php array using json_encode():

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

I get stuck at the ExposedPorts , please help. This is my php array but it does not work :

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

     'ExposedPorts'=>json_encode(["8080"=>"80"],JSON_FORCE_OBJECT)
]
5
  • 1
    What is the problem? Commented Apr 17, 2017 at 6:06
  • where is your php array?where you got stuck? Commented Apr 17, 2017 at 6:06
  • 1
    what do you mean by "stuck" ? Can you explain a lil'bit ? Commented Apr 17, 2017 at 6:09
  • i dont know how to create json object in ExposedPorts from php array Commented Apr 17, 2017 at 6:10
  • 1
    @spqa provide your php array please Commented Apr 17, 2017 at 6:12

1 Answer 1

1

Why not you json_encode the whole array like this,

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

    echo json_encode($array);

output:

{"Image":"wordpress","Env":["VIRTUAL_HOST=domain.com"],"ExposedPorts":{"8080":"80"}}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.