A recursive function to sort through parent and child nodes in PHP using a foreach loop on an array

前端 未结 2 1385
梦毁少年i
梦毁少年i 2021-01-07 10:18

I have a data set stored in an array that references itself with parent-child ids: id, parent_id, title etc. The top tier has a

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-07 10:48

    I doubt that you guys are still looking for a real answer to this, but it might help out others with the same problem. Below is a recursive function to resort an array placing children beneath parents.

    $initial = array(
        array(
            'name' => 'People',
            'ID' => 2,
            'parent' => 0
            ),
        array(
            'name' => 'Paul',
            'ID' => 4,
            'parent' => 2
            ),
        array(
            'name' => 'Liz',
            'ID' => 5,
            'parent' => 2
            ),
        array(
            'name' => 'Comus',
            'ID' => 6,
            'parent' => 3
            ),
        array(
            'name' => 'Mai',
            'ID' => 7,
            'parent' => 2
            ),
        array(
            'name' => 'Titus',
            'ID' => 8,
            'parent' => 3
            ),
        array(
            'name' => 'Adult',
            'ID' => 9,
            'parent' => 6
            ),
        array(
            'name' => 'Puppy',
            'ID' => 10,
            'parent' => 8
            ),
        array(
            'name' => 'Programmers',
            'ID' => 11,
            'parent' => 4
            )   ,
        array(
            'name' => 'Animals',
            'ID' => 3,
            'parent' => 0
            )                           
        );
    
    
    /*---------------------------------
    function parentChildSort_r
    $idField        = The item's ID identifier (required)
    $parentField    = The item's parent identifier (required)
    $els            = The array (required)
    $parentID       = The parent ID for which to sort (internal)
    $result     = The result set (internal)
    $depth          = The depth (internal)
    ----------------------------------*/
    
    function parentChildSort_r($idField, $parentField, $els, $parentID = 0, &$result = array(), &$depth = 0){
        foreach ($els as $key => $value):
            if ($value[$parentField] == $parentID){
                $value['depth'] = $depth;
                array_push($result, $value);
                unset($els[$key]);
                $oldParent = $parentID; 
                $parentID = $value[$idField];
                $depth++;
                parentChildSort_r($idField,$parentField, $els, $parentID, $result, $depth);
                $parentID = $oldParent;
                $depth--;
            }
        endforeach;
        return $result;
    }
    
    $result = parentChildSort_r('ID','parent',$initial);
    
    print '
    ';
    print_r($result);
    print '
    ';

    It's a wind down method that removes elements from the original array and places them into result set in the proper order. I made it somewhat generic for you, so it just needs you to tell it what your 'ID' field and 'parent' fields are called. Top level items are required to have a parent_id (however you name it) of 0. I also add a depth marker to each item so that you can format on output.

提交回复
热议问题