Skip to content

Instantly share code, notes, and snippets.

@vyspiansky
Last active September 9, 2024 14:33
Show Gist options
  • Select an option

  • Save vyspiansky/6552875 to your computer and use it in GitHub Desktop.

Select an option

Save vyspiansky/6552875 to your computer and use it in GitHub Desktop.

Revisions

  1. vyspiansky revised this gist Dec 30, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion build-tree.php
    Original 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);
    $tree = buildTreeFromArray($arrData);
  2. vyspiansky revised this gist Dec 30, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion build-tree.php
    Original 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`,
    * 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
  3. vyspiansky revised this gist Dec 30, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion build-tree.php
    Original 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']];
    $item['childs'] = $childs[$item['id']];

    return $childs[0];
    }
  4. vyspiansky revised this gist Dec 30, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions build-tree.php
    Original 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.
    * 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
    */
  5. vyspiansky revised this gist Dec 30, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion build-tree.php
    Original 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 an `id`, `name`, `parent_id`, where the `parent_id` is `0`
    * 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
  6. vyspiansky revised this gist Dec 30, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions build-tree.php
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@
    */

    // for object
    function buildTree($items) {
    function buildTreeFromObjects($items) {

    $childs = [];

    @@ -25,7 +25,7 @@ function buildTree($items) {
    }

    // or array version
    function buildTree($items) {
    function buildTreeFromArray($items) {

    $childs = [];

    @@ -62,4 +62,4 @@ function buildTree($items) {
    ['id' => 12, 'name' => 'Sledding', 'parent_id' => 10],
    ];

    $tree = buildTree($arrData);
    $tree = buildTreeFromArray($arrData);
  7. vyspiansky revised this gist Dec 30, 2019. 1 changed file with 26 additions and 3 deletions.
    29 changes: 26 additions & 3 deletions build-tree.php
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@
    // for object
    function buildTree($items) {

    $childs = array();
    $childs = [];

    foreach ($items as $item)
    $childs[$item->parent_id][] = $item;
    @@ -27,7 +27,7 @@ function buildTree($items) {
    // or array version
    function buildTree($items) {

    $childs = array();
    $childs = [];

    foreach ($items as &$item) $childs[$item['parent_id']][] = &$item;
    unset($item);
    @@ -39,4 +39,27 @@ function buildTree($items) {
    }

    // usage:
    $tree = buildTree($items);

    $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);
  8. vyspiansky revised this gist Dec 30, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions build-tree.php
    Original 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) {

  9. vyspiansky revised this gist Dec 30, 2019. 1 changed file with 3 additions and 25 deletions.
    28 changes: 3 additions & 25 deletions build-tree.php
    Original 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.
    * 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.
    *
    * Each item has an `id`, `name`, `parent_id`, where the `parent_id` is `0`
    * if it's top level.
    *
    * Source: http://goo.gl/p2GybZ
    */
  10. vyspiansky revised this gist Dec 30, 2019. 1 changed file with 31 additions and 1 deletion.
    32 changes: 31 additions & 1 deletion build-tree.php
    Original 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) {

  11. vyspiansky revised this gist Sep 22, 2019. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions build-tree.php
    Original file line number Diff line number Diff line change
    @@ -7,10 +7,10 @@ function buildTree($items) {

    $childs = array();

    foreach($items as $item)
    foreach ($items as $item)
    $childs[$item->parent_id][] = $item;

    foreach($items as $item) if (isset($childs[$item->id]))
    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;
    foreach ($items as &$item) $childs[$item['parent_id']][] = &$item;
    unset($item);

    foreach($items as &$item) if (isset($childs[$item['id']]))
    foreach ($items as &$item) if (isset($childs[$item['id']]))
    $item['childs'] = $childs[$item['id']];

    return $childs[0];
  12. @virua virua revised this gist Sep 13, 2013. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions build-tree.php
    Original file line number Diff line number Diff line change
    @@ -15,10 +15,7 @@ function buildTree($items) {

    return $childs[0];
    }
    // usage:
    $tree = buildTree($items);

    // array version
    // or array version
    function buildTree($items) {

    $childs = array();
    @@ -31,5 +28,6 @@ function buildTree($items) {

    return $childs[0];
    }

    // usage:
    $tree = buildTree($items);
  13. @virua virua created this gist Sep 13, 2013.
    35 changes: 35 additions & 0 deletions build-tree.php
    Original 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);