adjacency list into nested array PHP
There is the code

    <?php 
      global $table;
      $table = array(
        1=>0,
        2=>1,
        3=>2,
        4=>3,
        5=>4,
        6=>5
      );

      function get_ch($id){
        global $table;
        $ch = array_filter($table, function($parent) use($id) {
              return $parent == $id;
            }
        );
        return $ch;
      }

      function _rt($id) {  
        $arr= get_ch($id);
        foreach($arr as $id => $pid) {
            if(isset($arr[$id])) {
                $arr[$id] = _rt($id);
            }
        }
        return $arr;
      }
      
      $a = _rt(0);
      var_dump($a);


It's break my mind. The result is 

    array
    1 => 
      array
        2 => 
          array
            3 => 
              array
              
It's always has a three level of deep and never be deeper. Debugger show that all of $table has traversed but with the same strange things. Iam about giving up.

   
adjacency list into nested array PHP
===

There is the code

    <?php 
      global $table;
      $table = array(
        1=>0,
        2=>1,
        3=>2,
        4=>3,
        5=>4,
        6=>5
      );

      function get_ch($id){
        global $table;
        $ch = array_filter($table, function($parent) use($id) {
              return $parent == $id;
            }
        );
        return $ch;
      }

      function _rt($id) {  
        $arr= get_ch($id);
        foreach($arr as $id => $pid) {
            if(isset($arr[$id])) {
                $arr[$id] = _rt($id);
            }
        }
        return $arr;
      }
      
      $a = _rt(0);
      var_dump($a);


It's break my mind. The result is 

    array
    1 => 
      array
        2 => 
          array
            3 => 
              array
              
It's always has a three level of deep and never be deeper. Debugger show that all of $table has traversed but with the same strange things. Iam about giving up.

   