Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建php数组,然后使用MySQL数据转换为javascript数组_Javascript_Php_Mysql_Arrays_Database - Fatal编程技术网

创建php数组,然后使用MySQL数据转换为javascript数组

创建php数组,然后使用MySQL数据转换为javascript数组,javascript,php,mysql,arrays,database,Javascript,Php,Mysql,Arrays,Database,我试图使用MySQL数据库中的数据创建一个数组,这是使用treant js(我能找到的最好的方法)创建一个数据树,我得出的结论是,我必须先在php中创建数组,然后在json_encode()中创建它,以使其正常工作。我的问题是如何使用以下格式创建php数组,以及如何使用json_enconde转换它 构建数据树的javascript代码如下所示: Array approach var config = { container: "#collapsable-example",

我试图使用MySQL数据库中的数据创建一个数组,这是使用treant js(我能找到的最好的方法)创建一个数据树,我得出的结论是,我必须先在php中创建数组,然后在json_encode()中创建它,以使其正常工作。我的问题是如何使用以下格式创建php数组,以及如何使用json_enconde转换它

构建数据树的javascript代码如下所示:

 Array approach
var config = {
    container: "#collapsable-example",

    animateOnInit: true,

    node: {
        collapsable: true
    },
    animation: {
        nodeAnimation: "easeOutBounce",
        nodeSpeed: 700,
        connectorsAnimation: "bounce",
        connectorsSpeed: 700
    }
},
malory = {
    image: "img/malory.png"
},

lana = {
    parent: malory,
    image: "img/lana.png"
}

figgs = {
    parent: lana,
    image: "img/figgs.png"
}

sterling = {
    parent: malory,
    childrenDropLevel: 1,
    image: "img/sterling.png"
},

woodhouse = {
    parent: sterling,
    image: "img/woodhouse.png"
},

pseudo = {
    parent: malory,
    pseudo: true
},

cheryl = {
    parent: pseudo,
    image: "img/cheryl.png"
},

pam = {
    parent: pseudo,
    image: "img/pam.png"
},

chart_config = [config, malory, lana, figgs, sterling, woodhouse, pseudo, pam, cheryl];

提前感谢

因为您没有告诉我们有关mysql数据的任何信息,所以我只能告诉您,与上面显示的数组相当的php将是(部分):

json\u encode
然后将为woodhouse创建如下所示的json:

{
    parent: {
        parent: { /* malory data here */ },
        childrenDropLevel: 1,
        image: "img/sterling.png"
    },
    image: "img/woodhouse.png"
},
需要认识到的重要一点是,原始文件中的
stirling
对象也是递归编码的。JSON不支持像示例中那样的对象引用

我怀疑(但实际上不知道)您实际需要创建的JSON看起来更像来自数据库的数据——包括每个人的id和保存家长id的“家长”字段,而不是引用


一旦数据通过网络传输,您就可以在javascript客户机中创建带有引用的结构

首先,您需要正确拼写函数
json\u encode()
其次,它是一个对象而不是一个数组(
chart\u config
是一个数组)。这里有一个提示:
$config=newstdclass()发布问题时,你需要实际发布你尝试过的代码和你遇到的错误。当你可以直接声明一个对象数组时,定义一整堆变量然后将它们组装成这样的数组是毫无意义的。此外,Archer:1999即将发布!
{
    parent: {
        parent: { /* malory data here */ },
        childrenDropLevel: 1,
        image: "img/sterling.png"
    },
    image: "img/woodhouse.png"
},