-
-
Save MubinSayed/6ae47ef62038d0ae164adb6995fc3350 to your computer and use it in GitHub Desktop.
Revisions
-
ubermaniac renamed this gist
Feb 5, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
ubermaniac revised this gist
Feb 5, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,7 +26,7 @@ function convertToHierarchy($results, $idField='id', $parentIdField='parent', $c if (isset($itemReferences[$parentId])) { // parent exists $itemReferences[$parentId][$childrenField][$id] = $item; // assign item to parent $itemReferences[$id] =& $itemReferences[$parentId][$childrenField][$id]; // reference parent's item in single-dimentional array } elseif (!$parentId || !isset($hierarchy[$parentId])) { // -- parent Id empty or does not exist. Add it to the root $hierarchy[$id] = $item; $itemReferences[$id] =& $hierarchy[$id]; } -
ubermaniac revised this gist
Feb 5, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -40,7 +40,7 @@ function convertToHierarchy($results, $idField='id', $parentIdField='parent', $c if ( isset($itemReferences[$parentId] ) ) { // -- parent DOES exist $itemReferences[$parentId][$childrenField][$id] = $item; // -- assign it to the parent's list of children unset($hierarchy[$id]); // -- remove it from the root of the hierarchy } } -
ubermaniac renamed this gist
Feb 5, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
ubermaniac revised this gist
Feb 5, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ $results[] = array('id' => 'c', 'parent' => 'b', 'name' => 'Marky'); $results[] = array('id' => 'd', 'parent' => 'a', 'name' => 'Ricky'); $results[] = array('id' => 'e', 'parent' => '', 'name' => 'Timmy'); $results[] = array('id' => 'g', 'parent' => 'f', 'name' => 'Mary'); // -- Child is here before parent to demonstrate the second pass working $results[] = array('id' => 'f', 'parent' => 'e', 'name' => 'Tommy'); $results[] = array('id' => 'h', 'parent' => 'b', 'name' => 'Donny'); -
ubermaniac revised this gist
Feb 5, 2014 . 1 changed file with 20 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,37 +9,42 @@ $results[] = array('id' => 'c', 'parent' => 'b', 'name' => 'Marky'); $results[] = array('id' => 'd', 'parent' => 'a', 'name' => 'Ricky'); $results[] = array('id' => 'e', 'parent' => '', 'name' => 'Timmy'); $results[] = array('id' => 'g', 'parent' => 'f', 'name' => 'Mary'); $results[] = array('id' => 'f', 'parent' => 'e', 'name' => 'Tommy'); $results[] = array('id' => 'h', 'parent' => 'b', 'name' => 'Donny'); function convertToHierarchy($results, $idField='id', $parentIdField='parent', $childrenField='children') { $hierarchy = array(); // -- Stores the final data $itemReferences = array(); // -- temporary array, storing references to all items in a single-dimention foreach ( $results as $item ) { $id = $item[$idField]; $parentId = $item[$parentIdField]; if (isset($itemReferences[$parentId])) { // parent exists $itemReferences[$parentId][$childrenField][$id] = $item; // assign item to parent $itemReferences[$id] =& $itemReferences[$parentId][$childrenField][$id]; // reference parent's item in single-dimentional array } elseif (!$parentId || !isset($hierarchy[$parentId])) { // -- parent value Id empty. It is a root item $hierarchy[$id] = $item; $itemReferences[$id] =& $hierarchy[$id]; } } unset($results, $item, $id, $parentId); // -- Run through the root one more time. If any child got added before it's parent, fix it. foreach ( $hierarchy as $id => &$item ) { $parentId = $item[$parentIdField]; if ( isset($itemReferences[$parentId] ) ) { // -- parent DOES exist $itemReferences[$parentId][$childrenField][$id] = $item; // -- assign it to the parent's list of children unset($hierarchy[$id]); // -- remove it from the root of theId hierarchy } } unset($itemReferences, $id, $item, $parentId); return $hierarchy; } -
ubermaniac revised this gist
Feb 5, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ $results[] = array('id' => 'c', 'parent' => 'b', 'name' => 'Marky'); $results[] = array('id' => 'd', 'parent' => 'a', 'name' => 'Ricky'); $results[] = array('id' => 'e', 'parent' => '', 'name' => 'Timmy'); $results[] = array('id' => 'g', 'parent' => 'f', 'name' => 'Mary'); // -- child showing before parent, to demonstrate shuffling after initial pass $results[] = array('id' => 'f', 'parent' => 'e', 'name' => 'Tommy'); $results[] = array('id' => 'h', 'parent' => 'b', 'name' => 'Donny'); -
ubermaniac revised this gist
Feb 5, 2014 . 1 changed file with 9 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,8 +9,8 @@ $results[] = array('id' => 'c', 'parent' => 'b', 'name' => 'Marky'); $results[] = array('id' => 'd', 'parent' => 'a', 'name' => 'Ricky'); $results[] = array('id' => 'e', 'parent' => '', 'name' => 'Timmy'); $results[] = array('id' => 'g', 'parent' => 'f', 'name' => 'Mary'); $results[] = array('id' => 'f', 'parent' => 'e', 'name' => 'Tommy'); $results[] = array('id' => 'h', 'parent' => 'b', 'name' => 'Donny'); @@ -31,6 +31,14 @@ function convertToHierarchy($results) { } } // -- Run through the root one more time. If any child got added before it's parent, fix it. foreach ( $hierarchy as $id => &$h ) { if ( isset($itemReferences[$h['parent']] ) ) { // -- parent DOES exist $itemReferences[$h['parent']]['children'][$h['id']] = $h; // -- assign it to the parent's list of children unset($hierarchy[$id]); // -- remove it from the root of the hierarchy } } unset($itemReferences, $item, $results, $parent); return $hierarchy; -
ubermaniac revised this gist
Feb 5, 2014 . 1 changed file with 18 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,22 +14,28 @@ $results[] = array('id' => 'h', 'parent' => 'b', 'name' => 'Donny'); function convertToHierarchy($results) { $hierarchy = array(); // -- Stores the final data $itemReferences = array(); // -- temporary array, storing references to all items in a single-dimention foreach ( $results as $item ) { $parent = $item['parent']; if (isset($itemReferences[$parent])) { // parent exists $itemReferences[$parent]['children'][$item['id']] = $item; // assign item to parent $itemReferences[$item['id']] =& $itemReferences[$parent]['children'][$item['id']]; // reference parent's item in single-dimentional array } elseif (!$parent || !isset($hierarchy[$parent])) { // -- parent value is empty. It is a root item $hierarchy[$item['id']] = $item; $itemReferences[$item['id']] =& $hierarchy[$item['id']]; } } unset($itemReferences, $item, $results, $parent); return $hierarchy; } print_r(convertToHierarchy($results)); ?> -
ubermaniac revised this gist
Feb 5, 2014 . 1 changed file with 7 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,29 +8,25 @@ $results[] = array('id' => 'b', 'parent' => 'a', 'name' => 'Bobby'); $results[] = array('id' => 'c', 'parent' => 'b', 'name' => 'Marky'); $results[] = array('id' => 'd', 'parent' => 'a', 'name' => 'Ricky'); $results[] = array('id' => 'e', 'parent' => '', 'name' => 'Timmy'); $results[] = array('id' => 'f', 'parent' => 'e', 'name' => 'Tommy'); $results[] = array('id' => 'g', 'parent' => 'f', 'name' => 'Mary'); $results[] = array('id' => 'h', 'parent' => 'b', 'name' => 'Donny'); $hierarchy = array(); // -- Stores the final data $itemReferences = array(); // -- temporary array, storing references to all items in a single-dimention foreach ( $results as $item ) { $parent = $item['parent']; if (isset($itemReferences[$parent])) { // parent exists $itemReferences[$parent]['children'][$item['id']] = $item; // assign item to parent $itemReferences[$item['id']] =& $itemReferences[$parent]['children'][$item['id']]; // reference parent's item in single-dimentional array } elseif (!$parent || !isset($hierarchy[$parent])) { // -- parent value is empty. It is a root item $hierarchy[$item['id']] = $item; $itemReferences[$item['id']] =& $hierarchy[$item['id']]; } } -
ubermaniac created this gist
Feb 5, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,39 @@ <?php // -- Only one caveat : The results must be ordered so that an item's parent will be processed first. // -- Simulate a DB result $results = array(); $results[] = array('id' => 'a', 'parent' => '', 'name' => 'Johnny'); $results[] = array('id' => 'b', 'parent' => 'a', 'name' => 'Bobby'); $results[] = array('id' => 'c', 'parent' => 'b', 'name' => 'Marky'); $results[] = array('id' => 'd', 'parent' => 'a', 'name' => 'Ricky'); $results[] = array('id' => 'e', 'parent' => '', 'name' => 'Timmy'); $results[] = array('id' => 'f', 'parent' => 'e', 'name' => 'Tommy'); $results[] = array('id' => 'g', 'parent' => 'f', 'name' => 'Mary'); $results[] = array('id' => 'h', 'parent' => 'b', 'name' => 'Donny'); $hierarchy = array(); $itemReferences = array(); foreach ( $results as $item ) { $parent = $item['parent']; if (!$parent) { // a root item. Add it to the root! $hierarchy[$item['id']] = $item; $itemReferences[$item['id']] =& $hierarchy[$item['id']]; } else { if (isset($itemReferences[$parent])) { // parent exists $itemReferences[$parent]['children'][$item['id']] = $item; // assign item to parent $itemReferences[$item['id']] =& $itemReferences[$parent]['children'][$item['id']]; // reference parent's item in single-dimentional array } else { // -- non-root parent item must already exist die('blaahh... parent does not exist: ' . $parent); } } } unset($itemReferences, $item, $results, $parent); print_r($hierarchy);