Skip to content

Instantly share code, notes, and snippets.

@MubinSayed
Forked from ubermaniac/covertToHierarchy.php
Created April 12, 2019 12:32
Show Gist options
  • Save MubinSayed/6ae47ef62038d0ae164adb6995fc3350 to your computer and use it in GitHub Desktop.
Save MubinSayed/6ae47ef62038d0ae164adb6995fc3350 to your computer and use it in GitHub Desktop.

Revisions

  1. @ubermaniac ubermaniac renamed this gist Feb 5, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @ubermaniac ubermaniac revised this gist Feb 5, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion covertToHierarchy
    Original 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 value Id empty. It is a root item
    } elseif (!$parentId || !isset($hierarchy[$parentId])) { // -- parent Id empty or does not exist. Add it to the root
    $hierarchy[$id] = $item;
    $itemReferences[$id] =& $hierarchy[$id];
    }
  3. @ubermaniac ubermaniac revised this gist Feb 5, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion covertToHierarchy
    Original 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 theId hierarchy
    unset($hierarchy[$id]); // -- remove it from the root of the hierarchy
    }
    }

  4. @ubermaniac ubermaniac renamed this gist Feb 5, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. @ubermaniac ubermaniac revised this gist Feb 5, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.php
    Original 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');
    $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');

  6. @ubermaniac ubermaniac revised this gist Feb 5, 2014. 1 changed file with 20 additions and 15 deletions.
    35 changes: 20 additions & 15 deletions gistfile1.php
    Original 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'); // -- child showing before parent, to demonstrate shuffling after initial pass
    $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) {
    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 ) {
    $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']];
    $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 => &$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
    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, $item, $results, $parent);
    unset($itemReferences, $id, $item, $parentId);

    return $hierarchy;
    }
  7. @ubermaniac ubermaniac revised this gist Feb 5, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.php
    Original 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');
    $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');

  8. @ubermaniac ubermaniac revised this gist Feb 5, 2014. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion gistfile1.php
    Original 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' => 'f', 'parent' => 'e', 'name' => 'Tommy');
    $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;
  9. @ubermaniac ubermaniac revised this gist Feb 5, 2014. 1 changed file with 18 additions and 12 deletions.
    30 changes: 18 additions & 12 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -14,22 +14,28 @@
    $results[] = array('id' => 'h', 'parent' => 'b', 'name' => 'Donny');


    $hierarchy = array(); // -- Stores the final data
    function convertToHierarchy($results) {
    $hierarchy = array(); // -- Stores the final data

    $itemReferences = array(); // -- temporary array, storing references to all items in a single-dimention
    $itemReferences = array(); // -- temporary array, storing references to all items in a single-dimention

    foreach ( $results as $item ) {
    $parent = $item['parent'];
    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']];
    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;
    }

    unset($itemReferences, $item, $results, $parent);
    print_r(convertToHierarchy($results));

    print_r($hierarchy);
    ?>
  10. @ubermaniac ubermaniac revised this gist Feb 5, 2014. 1 changed file with 7 additions and 11 deletions.
    18 changes: 7 additions & 11 deletions gistfile1.php
    Original 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' => '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();
    $hierarchy = array(); // -- Stores the final data

    $itemReferences = array();
    $itemReferences = array(); // -- temporary array, storing references to all items in a single-dimention

    foreach ( $results as $item ) {
    $parent = $item['parent'];

    if (!$parent) { // a root item. Add it to the root!
    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']];
    } 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);
    }
    }
    }

  11. @ubermaniac ubermaniac created this gist Feb 5, 2014.
    39 changes: 39 additions & 0 deletions gistfile1.php
    Original 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);