0

I have some data from database which is like:

$name= "xxx";
$title= "xxx";
$arrayGender = array("Boy","Girl");
$arrayName = array("John", "Rosy");

How do I encode the data to a json object, so i guess the json would be something like:

{ 
  "name":"xxx",
  "title":"xxx",
  "children":[{"Boy","John"},{"Girl","Rosy"}]
}
1
  • Try json_encode to encode the data and set the content-type with header('Content-type: application/json'); Commented Sep 5, 2018 at 12:06

1 Answer 1

0

You can create an object and set your target data in it. Then use json_encode() to converting php object into json.

$name= "xxx";
$title= "xxx";
$arrayGender = array("Boy","Girl");
$arrayName = array("John", "Rosy");

$obj = new stdClass;
$obj->name = $name;
$obj->title = $title;
$obj->children = [$arrayGender, $arrayName];

echo json_encode($obj);

Check the code in demo

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.