So. There is an array with objects

$arr = array( (object)array("name"=>"Имя объекта1", "nameP"=> "" ), (object)array("name"=>"Имя объекта2", "nameP"=> "Имя объекта4" ), (object)array("name"=>"Имя объекта3", "nameP"=> "Имя объекта1" ), (object)array("name"=>"Имя объекта4", "nameP"=> "Имя объекта1" ), ); 

Where

  • name - the name of the object.
  • nameP is the name of its parent.

It is necessary to make it so that over this array it would create a multidimensional array, in which the hierarchy of arrays was observed:

 <?php Имя объекта1( Имя объекта => Имя объекта1 Дети => ( Имя объекта3 Имя объекта => Имя объекта3 Дети => () Имя объекта4 Имя объекта => Имя объекта4 Дети => ( Имя объекта2 Имя объекта => Имя объекта2 Дети => () ) ) ) 
  • Welcome to Stack Overflow in English. I corrected the format there a bit, you can click edit and see how it is done. - Nick Volynkin
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

OOP option:

1) A simple class that provides the addChild method and the ability to convert itself into an array:

 class TreeNode { public $id; public $parent; private $children; public function __construct($dataArray) { $this->id = $dataArray['id']; $this->parent = $dataArray['sub']; $this->children = array(); } public function addChild(TreeNode $node) { $this->children[$node->id] = $node; } public function toArray() { return array( 'id' => $this->id, 'sub' => $this->parent, 'children' => array_map( function(TreeNode $element) { return $element->toArray(); }, $this->children ) ); } } 

2) create an array of TreeNodes :

 $nodes = array(); $rootNodes = array(); foreach($ar as $arr) { $nodes[$ar['id']] = new TreeNode($ar); if ($ar['sub'] == 0) { $rootNodes[] = $nodes[$ar['id']]; } } 

3) adding descendants to the tree:

 array_map( function(TreeNode $element) use($nodes){ if(isset($nodes[$element->parent])) $nodes[$element->parent]->addChild($element); }, $nodes ); 

4) converting a tree into an array:

 print_r(array_map( function(TreeNode $element) { return $element->toArray(); }, $rootNodes) ); 

    If an array with objects was not created by us, then for the convenience of processing it should be supplemented with a list of child elements. And when printing the result, use print_r () instead of var_dump ().

    Program:

     $arr = [ (object)["name"=>"Имя объекта1","nameP"=> ""], (object)["name"=>"Имя объекта2","nameP"=> "Имя объекта4"], (object)["name"=>"Имя объекта3","nameP"=> "Имя объекта1"], (object)["name"=>"Имя объекта4","nameP"=> "Имя объекта1"] ]; function children(&$arr){ $arr2 = $arr; foreach($arr as $key=>&$item){ if($item->nameP == ""){ $root = $key; } $item->children = []; foreach($arr2 as $key2=>$item2){ if($item2->nameP == $item->name){ array_push($item->children, $key2); } } } return $root; }; function get_struct($arr, $index){ $node = $arr[$index]; struct == []; $struct[$node->name] = [ "Имя объекта" => $node->name, "Дети" => [] ]; foreach($node->children as $child){ $struct[$node->name]["Дети"][$arr[$child]->name] = get_struct($arr, $child); } return $struct; } var_dump($arr); $root = children($arr); var_dump($arr); $struct = get_struct($arr, $root); print("<pre>"); print_r($struct); print("</pre>"); 

    Results:

     array (size = 4)
       0 => 
         object (stdClass) [1]
           public 'name' => string 'Object1 name' (length = 22)
           public 'nameP' => string '' (length = 0)
       1 => 
         object (stdClass) [2]
           public 'name' => string 'Name of object2' (length = 22)
           public 'nameP' => string 'Name of object4' (length = 22)
       2 => 
         object (stdClass) [3]
           public 'name' => string 'Name of object3' (length = 22)
           public 'nameP' => string 'Object1 name' (length = 22)
       3 => 
         object (stdClass) [4]
           public 'name' => string 'The name of the object4' (length = 22)
           public 'nameP' => string 'Object1 name' (length = 22)
     array (size = 4)
       0 => 
         object (stdClass) [1]
           public 'name' => string 'Object1 name' (length = 22)
           public 'nameP' => string '' (length = 0)
           public 'children' => 
             array (size = 2)
               0 => int 2
               1 => int 3
       1 => 
         object (stdClass) [2]
           public 'name' => string 'Name of object2' (length = 22)
           public 'nameP' => string 'Name of object4' (length = 22)
           public 'children' => 
             array (size = 0)
               empty
       2 => 
         object (stdClass) [3]
           public 'name' => string 'Name of object3' (length = 22)
           public 'nameP' => string 'Object1 name' (length = 22)
           public 'children' => 
             array (size = 0)
               empty
       3 => 
         object (stdClass) [4]
           public 'name' => string 'The name of the object4' (length = 22)
           public 'nameP' => string 'Object1 name' (length = 22)
           public 'children' => 
             array (size = 1)
               0 => int 1
     Array
     (
         [Object Name1] => Array
             (
                 [Object Name] => Object Name1
                 [Children] => Array
                     (
                         [Object Name3] => Array
                             (
                                 [Object Name3] => Array
                                     (
                                         [Object Name] => Object Name3
                                         [Children] => Array
                                             (
                                             )
    
                                     )
    
                             )
    
                         [Object Name4] => Array
                             (
                                 [Object Name4] => Array
                                     (
                                         [Object Name] => Object Name4
                                         [Children] => Array
                                             (
                                                 [Object Name2] => Array
                                                     (
                                                         [Object Name2] => Array
                                                             (
                                                                 [Object Name] => Object Name2
                                                                 [Children] => Array
                                                                     (
                                                                     )
    
                                                             )
    
                                                     )
    
                                             )
    
                                     )
    
                             )
    
                     )
    
             )
    
     )