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.
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), );
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; }
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);
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 ) ) ) )
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!