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"}]
}
Rashedul Hasan
  • 193
  • 3
  • 13
  • Try json_encode to encode the data and set the content-type with header('Content-type: application/json'); – H45H Sep 05 '18 at 12:06

1 Answers1

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

Mohammad
  • 21,175
  • 15
  • 55
  • 84