Last active
September 9, 2024 14:33
-
-
Save vyspiansky/6552875 to your computer and use it in GitHub Desktop.
Revisions
-
vyspiansky revised this gist
Dec 30, 2019 . 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 @@ -62,4 +62,4 @@ function buildTreeFromArray($items) { ['id' => 12, 'name' => 'Sledding', 'parent_id' => 10], ]; $tree = buildTreeFromArray($arrData); -
vyspiansky revised this gist
Dec 30, 2019 . 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 @@ -4,7 +4,7 @@ * and turn it into a multidimensional array structure (tree), * where each item has an array of sub-items. * * Each item should have at least an `id` and `parent_id`, * where the `parent_id` is `0` if it's top level. * * Source: http://goo.gl/p2GybZ -
vyspiansky revised this gist
Dec 30, 2019 . 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 @@ -33,7 +33,7 @@ function buildTreeFromArray($items) { unset($item); foreach ($items as &$item) if (isset($childs[$item['id']])) $item['childs'] = $childs[$item['id']]; return $childs[0]; } -
vyspiansky revised this gist
Dec 30, 2019 . 1 changed file with 2 additions and 2 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 @@ -4,8 +4,8 @@ * and turn it into a multidimensional array structure (tree), * where each item has an array of sub-items. * * Each item has at least an `id` and `parent_id`, * where the `parent_id` is `0` if it's top level. * * Source: http://goo.gl/p2GybZ */ -
vyspiansky revised this gist
Dec 30, 2019 . 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 @@ -4,7 +4,7 @@ * and turn it into a multidimensional array structure (tree), * where each item has an array of sub-items. * * Each item has at least an `id` and `parent_id`, where the `parent_id` is `0` * if it's top level. * * Source: http://goo.gl/p2GybZ -
vyspiansky revised this gist
Dec 30, 2019 . 1 changed file with 3 additions and 3 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 @@ -11,7 +11,7 @@ */ // for object function buildTreeFromObjects($items) { $childs = []; @@ -25,7 +25,7 @@ function buildTree($items) { } // or array version function buildTreeFromArray($items) { $childs = []; @@ -62,4 +62,4 @@ function buildTree($items) { ['id' => 12, 'name' => 'Sledding', 'parent_id' => 10], ]; $tree = buildTreeFromArray($arrData); -
vyspiansky revised this gist
Dec 30, 2019 . 1 changed file with 26 additions and 3 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 @@ -13,7 +13,7 @@ // for object function buildTree($items) { $childs = []; foreach ($items as $item) $childs[$item->parent_id][] = $item; @@ -27,7 +27,7 @@ function buildTree($items) { // or array version function buildTree($items) { $childs = []; foreach ($items as &$item) $childs[$item['parent_id']][] = &$item; unset($item); @@ -39,4 +39,27 @@ function buildTree($items) { } // usage: $arrData = [ // Business ['id' => 1, 'name' => 'Business', 'parent_id' => 0], ['id' => 5, 'name' => 'Books and magazines', 'parent_id' => 1], ['id' => 6, 'name' => 'Electronics and telecom', 'parent_id' => 1], // Computers ['id' => 2, 'name' => 'Computers', 'parent_id' => 0], // Health ['id' => 3, 'name' => 'Health', 'parent_id' => 0], ['id' => 7, 'name' => 'Addictions', 'parent_id' => 3], ['id' => 8, 'name' => 'Dentistry', 'parent_id' => 3], ['id' => 9, 'name' => 'Vision Care', 'parent_id' => 3], // Sports ['id' => 4, 'name' => 'Sports', 'parent_id' => 0], ['id' => 10, 'name' => 'Winter Sports', 'parent_id' => 4], ['id' => 11, 'name' => 'Ice skating', 'parent_id' => 10], ['id' => 12, 'name' => 'Sledding', 'parent_id' => 10], ]; $tree = buildTree($arrData); -
vyspiansky revised this gist
Dec 30, 2019 . 1 changed file with 1 addition and 0 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 @@ -23,6 +23,7 @@ function buildTree($items) { return $childs[0]; } // or array version function buildTree($items) { -
vyspiansky revised this gist
Dec 30, 2019 . 1 changed file with 3 additions and 25 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 @@ -3,31 +3,9 @@ * The below functions take the single level array of items as an argument * and turn it into a multidimensional array structure (tree), * where each item has an array of sub-items. * * Each item has an `id`, `name`, `parent_id`, where the `parent_id` is `0` * if it's top level. * * Source: http://goo.gl/p2GybZ */ -
vyspiansky revised this gist
Dec 30, 2019 . 1 changed file with 31 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 @@ -1,7 +1,37 @@ <?php /** * The below functions take the single level array of items as an argument * and turn it into a multidimensional array structure (tree), * where each item has an array of sub-items. * It supposes that you have a flat data like this: $arrData = [ // Business ['id' => 1, 'name' => 'Business', 'parent_id' => 0], ['id' => 5, 'name' => 'Books and magazines', 'parent_id' => 1], ['id' => 6, 'name' => 'Electronics and telecom', 'parent_id' => 1], // Computers ['id' => 2, 'name' => 'Computers', 'parent_id' => 0], // Health ['id' => 3, 'name' => 'Health', 'parent_id' => 0], ['id' => 7, 'name' => 'Addictions', 'parent_id' => 3], ['id' => 8, 'name' => 'Dentistry', 'parent_id' => 3], ['id' => 9, 'name' => 'Vision Care', 'parent_id' => 3], // Sports ['id' => 4, 'name' => 'Sports', 'parent_id' => 0], ['id' => 10, 'name' => 'Winter Sports', 'parent_id' => 4], ['id' => 11, 'name' => 'Ice skating', 'parent_id' => 10], ['id' => 12, 'name' => 'Sledding', 'parent_id' => 10], ]; * Each item has an `id`, `name`, `parent_id`, where the `parent_id` is `0` if it's top level. * * Source: http://goo.gl/p2GybZ */ // for object function buildTree($items) { -
vyspiansky revised this gist
Sep 22, 2019 . 1 changed file with 4 additions and 4 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 @@ -7,10 +7,10 @@ function buildTree($items) { $childs = array(); foreach ($items as $item) $childs[$item->parent_id][] = $item; foreach ($items as $item) if (isset($childs[$item->id])) $item->childs = $childs[$item->id]; return $childs[0]; @@ -20,10 +20,10 @@ function buildTree($items) { $childs = array(); foreach ($items as &$item) $childs[$item['parent_id']][] = &$item; unset($item); foreach ($items as &$item) if (isset($childs[$item['id']])) $item['childs'] = $childs[$item['id']]; return $childs[0]; -
virua revised this gist
Sep 13, 2013 . 1 changed file with 2 additions and 4 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 @@ -15,10 +15,7 @@ function buildTree($items) { return $childs[0]; } // or array version function buildTree($items) { $childs = array(); @@ -31,5 +28,6 @@ function buildTree($items) { return $childs[0]; } // usage: $tree = buildTree($items); -
virua created this gist
Sep 13, 2013 .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,35 @@ <?php /* * Source: http://goo.gl/p2GybZ */ // for object function buildTree($items) { $childs = array(); foreach($items as $item) $childs[$item->parent_id][] = $item; foreach($items as $item) if (isset($childs[$item->id])) $item->childs = $childs[$item->id]; return $childs[0]; } // usage: $tree = buildTree($items); // array version function buildTree($items) { $childs = array(); foreach($items as &$item) $childs[$item['parent_id']][] = &$item; unset($item); foreach($items as &$item) if (isset($childs[$item['id']])) $item['childs'] = $childs[$item['id']]; return $childs[0]; } // usage: $tree = buildTree($items);