我正在尝试创建一个 javascript 对象作为引导树视图的输入。我有 php 从 mysql 获取数据,并将结果编码为以下结构:
{"Company 1":{"Production":["Brands","Categories","Products","Stocks"],"Sales":["Customers","Orders","Staffs","Stores"]},"Company 2":{"Production":["Brands","Categories","Products","Stocks"],"Sales":["Customers","Orders","Staffs","Stores"]}}
生成该 json 的 PHP 代码:
$databases=[];
foreach($result as $row){
$database=$row["database"];
$schema=$row["schema"];
$table=$row["object"];
if(!array_key_exists($database, $databases))
$databases[$database]=[];
if(!array_key_exists($schema, $databases[$database]))
$databases[$database][$schema]=[];
array_push($databases[$database][$schema], $table);
}
echo json_encode($databases);
但我正在努力将该 json 结构放入所需的 JavaScript 对象的嵌套数组中。以下是所需的结构:
[ { text: "Company 1", nodes: [ { text: "Production", nodes: [ { text: "Brands" }, { text: "Categories" }, { text: "Products" }, { text: "Stocks" } ] }, { text: "Sales", nodes: [ { text: "Customers" }, { text: "Orders" }, { text: "Staffs" }, { text: "Stores" } ] } ] }, { text: "Company 2", nodes: [ { text: "Production", nodes: [ { text: "Brands" }, { text: "Categories" }, { text: "Products" }, { text: "Stocks" } ] }, { text: "Sales", nodes: [ { text: "Customers" }, { text: "Orders" }, { text: "Staffs" }, { text: "Stores" } ] } ] } ];
任何建议,将不胜感激
冉冉说
温温酱