Wednesday 15 January 2014

php - Recursive function to generate multidimensional array from database result -



php - Recursive function to generate multidimensional array from database result -

i'm looking write function takes array of pages/categories (from flat database result) , generates array of nested page/category items based on parent ids. recursively, level of nesting can done.

for example: i'm fetching pages in 1 query, , database table looks like

+-------+---------------+---------------------------+ | id | parent_id | title | +-------+---------------+---------------------------+ | 1 | 0 | parent page | | 2 | 1 | sub page | | 3 | 2 | sub sub page | | 4 | 0 | parent page | +-------+---------------+---------------------------+

and array end process in view files:

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 ) ) ) ) ) [1] => array ( [id] => 4 [parent_id] => 0 [title] => parent page ) )

i've looked , tried every solution i've come across (there's lot of them here on stack overflow, have had no luck getting generic plenty work both pages , categories.

here's closest i've gotten, doesn't work because i'm assigning children first level parent.

function page_walk($array, $parent_id = false) { $organized_pages = array(); $children = array(); foreach($array $index => $page) { if ( $page['parent_id'] == 0) // no, spit out , you're done { $organized_pages[$index] = $page; } else // if does, { $organized_pages[$parent_id]['children'][$page['id']] = $this->page_walk($page, $parent_id); } } homecoming $organized_pages; } function page_list($array) { $fakepages = array(); $fakepages[0] = array('id' => 1, 'parent_id' => 0, 'title' => 'parent page'); $fakepages[1] = array('id' => 2, 'parent_id' => 1, 'title' => 'sub page'); $fakepages[2] = array('id' => 3, 'parent_id' => 2, 'title' => 'sub sub page'); $fakepages[3] = array('id' => 4, 'parent_id' => 3, 'title' => 'another parent page'); $pages = $this->page_walk($fakepages, 0); print_r($pages); }

some simple, generic tree building:

function buildtree(array $elements, $parentid = 0) { $branch = array(); foreach ($elements $element) { if ($element['parent_id'] == $parentid) { $children = buildtree($elements, $element['id']); if ($children) { $element['children'] = $children; } $branch[] = $element; } } homecoming $branch; } $tree = buildtree($rows);

the algorithm pretty simple:

take array of elements , id of current parent (initially 0/nothing/null/whatever). loop through elements. if parent_id of element matches current parent id got in 1., element kid of parent. set in list of current children (here: $branch). call function recursively id of element have identified in 3., i.e. find children of element, , add together them children element. return list of found children.

in other words, 1 execution of function returns list of elements children of given parent id. phone call buildtree($myarray, 1), homecoming list of elements have parent id 1. function called parent id beingness 0, elements without parent id returned, root nodes. function calls recursively find children of children.

php arrays function recursion

No comments:

Post a Comment