Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.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
使用mysql结果从多维PHP数组构建树_Php_Mysql_Arrays_Recursion_Multidimensional Array - Fatal编程技术网

使用mysql结果从多维PHP数组构建树

使用mysql结果从多维PHP数组构建树,php,mysql,arrays,recursion,multidimensional-array,Php,Mysql,Arrays,Recursion,Multidimensional Array,我想创建一个函数,该函数递归地从具有未知级别和未知元素计数的多维数组生成树视图 这是数据库文件 -- -- Table structure for table `accounts_tree` -- CREATE TABLE `accounts_tree` ( `id` bigint(20) NOT NULL, `parent_id` bigint(20) DEFAULT '0', `final_acc_id` bigint(20) NOT NULL, `Code` varcha

我想创建一个函数,该函数递归地从具有未知级别和未知元素计数的多维数组生成树视图

这是数据库文件

--
-- Table structure for table `accounts_tree`
--

CREATE TABLE `accounts_tree` (
  `id` bigint(20) NOT NULL,
  `parent_id` bigint(20) DEFAULT '0',
  `final_acc_id` bigint(20) NOT NULL,
  `Code` varchar(255) DEFAULT NULL,
  `name_a` varchar(255) DEFAULT NULL,
  `name_e` varchar(255) DEFAULT NULL,
  `nature` tinyint(1) DEFAULT '0',
  `currency_id` bigint(20) NOT NULL DEFAULT '0',
  `currency_rate` varchar(200) DEFAULT NULL,
  `match_date` date DEFAULT NULL,
  `notes` text,
  `created` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `accounts_tree`
--

INSERT INTO `accounts_tree` (`id`, `parent_id`, `final_acc_id`, `Code`, `name_a`, `name_e`, `nature`, `currency_id`, `currency_rate`, `match_date`, `notes`, `created`) VALUES
(1, 0, 1, '1', 'folder 1', 'budget', 0, 1, '1', NULL, NULL, '2017-12-16 00:00:00'),
(2, 0, 1, '1', 'folder 2', 'budget2', 0, 1, '1', NULL, NULL, '2017-12-16 00:00:00'),
(3, 1, 1, '1', 'sub 1-1', 'balance sheet', 0, 1, '1', NULL, NULL, '2017-12-16 00:00:00'),
(4, 2, 1, '1', 'sub 2-1', 'balance sheet', 0, 1, '1', NULL, NULL, '2017-12-16 00:00:00'),
(5, 3, 1, '1', 'Sub 1-1-1', 'balance sheet', 0, 1, '1', NULL, NULL, '2017-12-16 00:00:00'),
(6, 0, 1, '3', 'folder 3', 'balance sheet', 0, 1, '1', NULL, NULL, '2017-12-16 00:00:00'),
(7, 5, 1, '3', 'sub 1-1-1-1', 'balance sheet', 0, 1, '1', NULL, NULL, '2017-12-16 00:00:00');
PHP用于生成数组

$query = "SELECT * FROM `accounts_tree`";
$result = $db->query($query);
$output = array();
while ($row = $db->fetch_assoc($result)) {
    $sub_data["id"] = $row["id"];
    $sub_data["name"] = $row["name_a"];
    $sub_data["parent_id"] = $row["parent_id"];
    $data[] = $sub_data;
}
foreach ($data as $key => &$value) {
    $output[$value["id"]] = &$value;
}
foreach ($data as $key => &$value) {
    if ($value["parent_id"] && isset($output[$value["parent_id"]])) {
        $output[$value["parent_id"]]["nodes"][] = &$value;
    }
}
foreach ($data as $key => & $value ) {
    if ($value["parent_id"] && isset($output[$value["parent_id"]])) {
        unset($data[$key]);
    }
}
编辑

输出数组将是

Array
(
    [0] => Array
        (
            [id] => 1,
            [name] => "folder 1",
            [parent_id] => 0,
            [nodes] => Array
                (
                    [0] => Array
                        (
                            [id] => 3,
                            [name] => "sub 1-1",
                            [parent_id] => 1,
                            [nodes] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 5,
                                            [name] => "Sub 1-1-1",
                                            [parent_id] => 3,
                                            [nodes] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 7,
                                                            [name] => "sub1-1-1-1",
                                                            [parent_id] => 5
                                                        )

                                                )

                                        )

                                )

                        )

                )

        ),

    [1] => Array
        (
            [id] => 2,
            [name] => "folder 2",
            [parent_id] => 0,
            [nodes] => Array
                (
                    [0] => Array
                        (
                            [id] => 4,
                            [name] => "sub 2-1",
                            [parent_id] => 2
                        )

                )

        ),

    [5] => Array
        (
            [id] => 6,
            [name] => "folder 3",
            [parent_id] => 0
        )
    );
我们只需要收集名称并将其放在树状视图中 请任何人都能解决这个问题:S


谢谢

解释可以在内联评论中找到。此函数提供精确的所需输出。还要注意我为设置
$resultset
而重写的查询

代码:()

输出:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => folder 1
            [parent_id] => 0
            [nodes] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [name] => sub 1-1
                            [parent_id] => 1
                            [nodes] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 5
                                            [name] => Sub 1-1-1
                                            [parent_id] => 3
                                            [nodes] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 7
                                                            [name] => sub 1-1-1-1
                                                            [parent_id] => 5
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 2
            [name] => folder 2
            [parent_id] => 0
            [nodes] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [name] => sub 2-1
                            [parent_id] => 2
                        )

                )

        )

    [5] => Array
        (
            [id] => 6
            [name] => folder 3
            [parent_id] => 0
        )

)

解释可以作为内联注释找到。此函数提供精确的所需输出。还要注意我为设置
$resultset
而重写的查询

代码:()

输出:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => folder 1
            [parent_id] => 0
            [nodes] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [name] => sub 1-1
                            [parent_id] => 1
                            [nodes] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 5
                                            [name] => Sub 1-1-1
                                            [parent_id] => 3
                                            [nodes] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 7
                                                            [name] => sub 1-1-1-1
                                                            [parent_id] => 5
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 2
            [name] => folder 2
            [parent_id] => 0
            [nodes] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [name] => sub 2-1
                            [parent_id] => 2
                        )

                )

        )

    [5] => Array
        (
            [id] => 6
            [name] => folder 3
            [parent_id] => 0
        )

)

数据库表返回行。行不适合保存多维数据。一种更合理的方法是创建几个表,您可以使用一个公共值将它们连接在一起。@miknik我用PHP生成多维数组,而不是从database@mickmackusa完成!我希望你能解决这个问题~thanks@mickmackusa你写的代码很好,但你没有理解我的问题可能我解释得不好,但你做得很好,谢谢我想处理出来,在
ul
its
li
中用namesA数据库表返回行进行排序。行不适合保存多维数据。一种更合理的方法是创建几个表,您可以使用一个公共值将它们连接在一起。@miknik我用PHP生成多维数组,而不是从database@mickmackusa完成!我希望你能解决这个问题~thanks@mickmackusa你写的代码很好,但你不理解我的问题可能我解释得不好,但你做得很好,谢谢我想处理出来,在
ul
its
li
中用名称进行排序