为了账号安全,请及时绑定邮箱和手机立即绑定

Json 到 Javascript 对象数组

Json 到 Javascript 对象数组

PHP
紫衣仙女 2023-07-21 18:16:02
我正在尝试创建一个 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" } ] } ] } ];任何建议,将不胜感激
查看完整描述

2 回答

?
冉冉说

TA贡献1877条经验 获得超1个赞

我建议使用递归flatItem函数来做到这一点。我还介绍了一个array_map_with_keys辅助函数,因为 PHP 自己的array_map函数会忽略键,而在您的情况下,我们需要它。



function array_map_with_keys($callback, $array){

  return array_map($callback, array_keys($array), $array);

}


function flatItem($key, $value) {

  if(is_array($value)){

    return 

      ["text" => $key,

       "nodes" => array_map_with_keys("flatItem", $value)

      ];  

  }else{

    return ["text" => $value];

  }

}


$converted = array_map_with_keys("flatItem", $databases);


echo json_encode($converted);

结果就是你所期望的:


[{"text":"Company 1","nodes":[{"text":"Production","nodes":[{"te

xt":"Brands"},{"text":"Categories"},{"text":"Products"},{"text":

"Stocks"}]},{"text":"Sales","nodes":[{"text":"Customers"},{"text

":"Orders"},{"text":"Staffs"},{"text":"Stores"}]}]},{"text":"Com

pany 2","nodes":[{"text":"Production","nodes":[{"text":"Brands"}

,{"text":"Categories"},{"text":"Products"},{"text":"Stocks"}]},{

"text":"Sales","nodes":[{"text":"Customers"},{"text":"Orders"},{

"text":"Staffs"},{"text":"Stores"}]}]}]

你可以把它传递给js。


查看完整回答
反对 回复 2023-07-21
?
温温酱

TA贡献1752条经验 获得超4个赞

const data = {

  "Company 1": {

    "Production": ["Brands", "Categories", "Products", "Stocks"],

    "Sales": ["Customers", "Orders", "Staffs", "Stores"]

  },

  "Company 2": {

    "Production": ["Brands", "Categories", "Products", "Stocks"],

    "Sales": ["Customers", "Orders", "Staffs", "Stores"]

  }

}


function converter(data) {

  return Object.entries(data).reduce((converted, [key, val]) => {

    const element = {

      text: key,

      nodes: [...Object.entries(val).map(([key2, val2]) => {

        return {

          text: key2,

          nodes: [...Object.values(val2).map(val3 => {

            return {

              text: val3

            }

          })]

        }

      })]

    }

    converted.push(element);

    return converted

  }, []);

}


console.log(converter(data))


查看完整回答
反对 回复 2023-07-21
  • 2 回答
  • 0 关注
  • 129 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信