Home >Backend Development >PHP Tutorial >How to Recursively Build a Multidimensional Array from Flat Database Results?

How to Recursively Build a Multidimensional Array from Flat Database Results?

Barbara Streisand
Barbara StreisandOriginal
2024-12-14 01:40:10260browse

How to Recursively Build a Multidimensional Array from Flat Database Results?

Building a Multidimensional Array from Database Results Recursively

To retrieve hierarchical data structures, such as page and category menus, from a flat database result, a recursive function can be employed. This function will take the original array and organize it into nested arrays based on parent-child relationships.

The Function:

function buildTree(array $elements, $parentId = 0) {
    $branch = array();

    foreach ($elements as $element) {
        if ($element['parent_id'] == $parentId) {
            $children = buildTree($elements, $element['id']);
            if ($children) {
                $element['children'] = $children;
            }
            $branch[] = $element;
        }
    }

    return $branch;
}

How it Works:

  1. The function takes in an array of elements and an optional parent ID, defaulting to 0.
  2. It iterates over each element in the array.
  3. If an element's parent ID matches the specified parent ID, it is considered a child of that parent.
  4. The function recursively calls itself to find all children of the current child element.
  5. If the element has any children, they are added as an array under the 'children' key of the current element.
  6. Finally, the function returns the array of child elements for the given parent.

Example Usage:

To convert the example database result into a multidimensional array using this function:

$tree = buildTree($rows);

Where $rows is the original array of database results.

Output:

The resulting hierarchical array will resemble the desired output:

Array
(
    [0] => Array
        (
            [id] => 1
            [parent_id] => 0
            [title] => Parent Page
            [children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 2
                                    [parent_id] => 1
                                    [title] => Sub Page
                                    [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 3
                                                            [parent_id] => 1
                                                            [title] => Sub Sub Page
                                                        )
                                                )
                                )
                        )
            [children] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 4
                                    [parent_id] => 0
                                    [title] => Another Parent Page
                                    [children] => Array
                                                (

                                                )
                                )
                        )
        )
)

The above is the detailed content of How to Recursively Build a Multidimensional Array from Flat Database Results?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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