我从数据库中获得了一些数据,例如:
$name= "xxx";
$title= "xxx";
$arrayGender = array("Boy","Girl");
$arrayName = array("John", "Rosy");
我如何将数据编码为json对象,所以我想json类似于:
{
"name":"xxx",
"title":"xxx",
"children":[{"Boy","John"},{"Girl","Rosy"}]
}
答案 0 :(得分:0)
您可以创建一个对象并在其中设置目标数据。然后使用json_encode()
将php对象转换为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);
检查demo中的代码