Current Location: Home> Latest Articles> How to Convert List to Tree Structure in PHP | Data Transformation and Management Techniques

How to Convert List to Tree Structure in PHP | Data Transformation and Management Techniques

gitbox 2025-07-27

How to Convert List to Tree Structure in PHP

During development, it's often necessary to convert flat data structures into tree structures, especially in scenarios that require displaying hierarchical relationships, such as menus, categories, organizational charts, etc. In this article, we'll walk through how to use PHP to transform list data into a tree structure, enhancing your development efficiency.

Basic Concepts of Tree Structure

A tree structure is a hierarchical data model made up of a root node and multiple child nodes. Each child node can have its own child nodes, and so on. Tree structures are ideal for representing hierarchical relationships like organizational structures or product categories.

Prepare the Data

To convert a flat list into a tree structure, the first step is to prepare a dataset that includes information about each node and its parent node. Below is an example of such a dataset:

// Example data
$list = [
    ['id' => 1, 'name' => 'Root Node', 'pid' => 0],
    ['id' => 2, 'name' => 'Child Node 1', 'pid' => 1],
    ['id' => 3, 'name' => 'Child Node 2', 'pid' => 1],
    ['id' => 4, 'name' => 'Child Node 1.1', 'pid' => 2],
    ['id' => 5, 'name' => 'Child Node 1.2', 'pid' => 2],
];

PHP Function for List to Tree Structure Conversion

Next, we’ll create a PHP function that converts the flat list into a tree structure. The core logic of the function is to iterate through the data and establish hierarchical relationships between the node IDs and parent node IDs:

function listToTree($list) {
    $tree = [];
    $references = [];
    // Set up references for each node
    foreach ($list as $element) {
        $references[$element['id']] = $element;
        $references[$element['id']]['children'] = [];
    }
    // Build the tree structure
    foreach ($list as $element) {
        if ($element['pid'] == 0) {
            // Root node
            $tree[] = &$references[$element['id']];
        } else {
            // Child node
            $references[$element['pid']]['children'][] = &$references[$element['id']];
        }
    }
    return $tree;
}

// Call the function
$tree = listToTree($list);
print_r($tree);

Code Analysis

In the code above, we define a function called listToTree that takes a flat list of node data as input and returns a tree structure. The function uses an array of references to keep track of each node and its children, ultimately constructing the tree structure.

Outputting the Tree Structure

To view the resulting tree structure, we can use the print_r function. You can further process this structure based on your needs, such as converting it to JSON format or displaying it on a web page.

Conclusion

This article explained how to convert list data to tree structure in PHP. By understanding the basic concepts of tree structures and the implementation details, you can easily master this technique and use it for hierarchical data presentation and management. Mastering these skills will not only improve your development efficiency but also make data management more convenient.

We hope this article was helpful. If you have any questions, feel free to leave a comment below for further discussion.