Home > Backend Development > PHP Tutorial > Use of PHP array grouping functions in creating hierarchical structures

Use of PHP array grouping functions in creating hierarchical structures

WBOY
Release: 2024-05-01 10:06:02
Original
739 people have browsed it

PHP array grouping functions can be used to create hierarchical structures by grouping array elements according to specified keys, resulting in arrays with multiple levels of nesting. The code example uses array_group_by() to group data and then converts it into a hierarchy using the recursive function createHierarchy(). This function iterates through the grouped results, creates sub-levels based on parent_id, and ultimately returns a nested array with a nice hierarchical structure.

PHP 数组分组函数在创建层级结构中的使用

Application of PHP array grouping function in creating hierarchical structure

Array grouping function in processing scenarios where data hierarchy needs to be maintained Very useful in . In PHP, the array_group_by function can create nested arrays with multiple levels by grouping elements in an array based on a specified key.

Code example

<?php

// 有一个包含 id 和 parent_id 的表数据
$tableData = [
    ['id' => 1, 'parent_id' => null],
    ['id' => 2, 'parent_id' => 1],
    ['id' => 3, 'parent_id' => 2],
    ['id' => 4, 'parent_id' => 1],
    ['id' => 5, 'parent_id' => 4],
];

// 根据 parent_id 对表数据进行分组
$groupedData = array_group_by($tableData, 'parent_id');

// 递归函数用于将分组结果转换为层级结构
function createHierarchy($data, $parentId = null) {
    $result = [];
    foreach ($data as $id => $values) {
        if ($id == $parentId) {
            foreach ($values as $value) {
                $result[] = [
                    'id' => $value['id'],
                    'value' => $value['value'],
                    'children' => createHierarchy($data, $value['id']),
                ];
            }
            return $result;
        }
    }
}

// 将分组数据转换为层级结构
$hierarchy = createHierarchy($groupedData, null);

// 打印层级结构
print_r($hierarchy);
Copy after login

Output result

Array
(
    [0] => Array
        (
            [id] => 1
            [value] => 1
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [value] => 2
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 3
                                            [value] => 3
                                            [children] => Array
                                                (
                                                )
                                        )
                                )
                        )
                    [1] => Array
                        (
                            [id] => 4
                            [value] => 4
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 5
                                            [value] => 5
                                            [children] => Array
                                                (
                                                )
                                        )
                                )
                        )
                )
        )
)
Copy after login

In this example, the createHierarchy function is used The recursive algorithm traverses the grouped data and creates sub-levels based on parent_id. Ultimately, it returns a nested PHP array with a nice hierarchical structure.

The above is the detailed content of Use of PHP array grouping functions in creating hierarchical structures. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template