Home > Backend Development > PHP Problem > How to convert php array to tree array

How to convert php array to tree array

zbt
Release: 2023-08-04 17:30:48
Original
1338 people have browsed it

How to convert php array to tree array: 1. Understand the structure and concept of tree array. In tree array, each element has a unique identifier and an identifier of the parent element. ; 2. Prepare the example array, create a normal associative array, which contains some items and their parent element identifiers; 3. Create a conversion function, which will create a function to convert the normal array into a tree array; 4. Just call the conversion function and output the result.

How to convert php array to tree array

The operating environment of this tutorial: windows10 system, php8.1.3 version, DELL G3 computer.

In PHP, array is a very common and practical data structure. Sometimes, we need to convert ordinary arrays into tree arrays to organize and display data more conveniently. This article will introduce how to convert a PHP array into a tree array.

1. Understand the structure and concepts of tree arrays

In a tree array, each element has a unique identifier and an identification of the parent element symbol. The identifier of the parent element can be matched with the identifier of the child element to determine the hierarchical relationship between elements. The structure of a tree array is similar to a tree structure, where the top element is called the root element and the child elements can be leaf nodes or other branch nodes.

2. Prepare the example array

As an example, we create a normal associative array containing some items and their parent element identifiers as follows:

$data = array(
array('id' => 1, 'name' => '项目1', 'parent_id' => 0),
array('id' => 2, 'name' => '项目1.1', 'parent_id' => 1),
array('id' => 3, 'name' => '项目1.2', 'parent_id' => 1),
array('id' => 4, 'name' => '项目1.1.1', 'parent_id' => 2),
array('id' => 5, 'name' => '项目2', 'parent_id' => 0),
array('id' => 6, 'name' => '项目2.1', 'parent_id' => 5),
array('id' => 7, 'name' => '项目2.2', 'parent_id' => 5),
);
Copy after login

The above array contains some items, where the element with parent element identifier 0 is the root element.

3. Create a conversion function

Next, we will create a function to convert an ordinary array into a tree array. First, we need to define a recursive function to traverse the array and build the tree. The function will receive two parameters: the input array and the parent element identifier.

function buildTree(array $data, $parentId = 0) {
$tree = array();
foreach ($data as $row) {
if ($row['parent_id'] == $parentId) {
$children = buildTree($data, $row['id']);
if ($children) {
$row['children'] = $children;
}
$tree[] = $row;
}
}
return $tree;
}
Copy after login

In the above code, we first create an empty array $tree, and then use a foreach loop to traverse the input array $data. If the element's parent element identifier is the same as the parent element identifier passed in, a child array named $children is created and the buildTree() function will be called recursively to add the child elements to $children. If $children is not an empty array, append it to the "children" key of the current row. Finally, add the current row to the $tree array.

4. Call the conversion function and output the result

Next, we just call the buildTree() function and pass the sample array as a parameter. We can then use the print_r() function or the var_dump() function to print the results to verify whether the conversion was successful.

$treeData = buildTree($data);
print_r($treeData);
Copy after login

Run the above code, you will see the output of the tree array:

Array
(
[0] => Array
(
[id] => 1
[name] => 项目1
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[id] => 2
[name] => 项目1.1
[parent_id] => 1
[children] => Array
(
[0] => Array
(
[id] => 4
[name] => 项目1.1.1
[parent_id] => 2
)
)
)
[1] => Array
(
[id] => 3
[name] => 项目1.2
[parent_id] => 1
)
)
)
[1] => Array
(
[id] => 5
[name] => 项目2
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[id] => 6
[name] => 项目2.1
[parent_id] => 5
)
[1] => Array
(
[id] => 7
[name] => 项目2.2
[parent_id] => 5
)
)
)
)
Copy after login

As you can see, we successfully converted the ordinary array into a tree array.

Summary

This article introduces how to convert a PHP array into a tree array. By understanding the structure and concepts of tree arrays, create a recursive function to build the tree, and call the function to transform the array. In this way, you can easily organize and display the data

The above is the detailed content of How to convert php array to tree array. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template