在创建用于 jstree 的有效 JSON 对象时出现问题

Getting issues creating valid JSON object to use for jstree

我正在尝试使用 jstree 创建一棵树,它将显示特定数据库的所有 table 以及 table 的列。

现在这是我的脚本,我用它首先从数据库中获取 table 名称,然后使用该 table 名称获取该 [=] 中可用的所有列32=].

<?php
    $servername = "localhost";
    $username = "test";
    $password = "test";
    $dbname = "test";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $post_data = array('id' => $dbname,'text' => $dbname, 'children' => array());

    //echo "Connected successfully";

    $sql = "SHOW tables";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            $new = array();
            array_push($new, array("id" => $row['Tables_in_test'], "text" => $row['Tables_in_test'], "children" => array()));

            $sql1 = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '".$dbname."' AND TABLE_NAME = '".$row['Tables_in_test']."'";
            $result1 = $conn->query($sql1);
            while($row1 = $result1->fetch_assoc()) {
                array_push($new[0]['children'], array("id" => $row1['COLUMN_NAME'], "text" => $row1['COLUMN_NAME']));
            }

            array_push($post_data['children'], $new);
        }
    } else {
        echo "0 results";
    }
    print_r(json_encode($post_data));

    $conn->close();
?> 

现在,我通过以下方式获取数据:

{
"id": "test",
"text": "test",
"children": [
  [
    {
      "id": "accounts",
      "text": "accounts",
      "children": [
        {
          "id": "id",
          "text": "id"
        },
        {
          "id": "name",
          "text": "name"
        }
      ]
    },
    {
        "id":"accounts_cases",
        "text":"accounts_cases",
        "children":[
        {
          "id":"id",
          "text":"id"
        },
        {
          "id":"account_id",
          "text":"account_id"
        }
      ]
    }
  ],
],
}

现在,这种格式的数据不适用于 jstree。如您所见,第一个 children 是一个数组,但由于脚本,它以某种方式将数组显示为数组。是这样的:

"text": "test",
"children": [
 [
    "id": "accounts",

应该是这样的:

"text": "test",
"children": [
    "id": "accounts",

我不知道如何解释这个,但应该是这样才能正常工作:

{
"id": "sugarcrm",
"text": "sugarcrm",
"children": [
    {
      "id": "accounts",
      "text": "accounts",
      "children": [
        {
          "id": "id",
          "text": "id"
        },
        {
          "id": "name",
          "text": "name"
        }
      ]
    },
    {
        "id":"accounts_cases",
        "text":"accounts_cases",
        "children":[
        {
          "id":"id",
          "text":"id"
        },
        {
          "id":"account_id",
          "text":"account_id"
        }
      ]
    }
],
}

我知道我的脚本有问题,但我无法更正。所以,请在这里帮助我。

经过一番day/night的斗争,我终于找到了解决办法。我发现在同一个主数组中我可以将新数组推入其中,而不是使用另一个数组 $new。在这里找到以下代码:

if ($result->num_rows > 0) {
    // output data of each row
    $i = 0;
    while($row = $result->fetch_assoc()) {

       array_push($post_data['children'], array("id" => $row['Tables_in_test'], "text" => $row['Tables_in_test'], "children" => array()));

       $sql1 = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '".$dbname."' AND TABLE_NAME = '".$row['Tables_in_test']."'";

       $result1 = $conn->query($sql1);

       while($row1 = $result1->fetch_assoc()) {

          array_push($post_data['children'][$i]['children'], array("id" => $row1['COLUMN_NAME'], "text" => $row1['COLUMN_NAME']));
       }
       $i++;
    }
} else {
    echo "0 results";
}

在上面的代码中,使用 $i 计数器来检查获取最新推送的数组,如您所知,array_push 插入数组并将键设置为整数,如 0、1、.. .等等。

希望对遇到类似问题的人有所帮助。