In PHP, I get an array of objects. Each object is either a parent or descendant. Each object has a unique ID. But descendants have PARENT_ID - which corresponds to the ID of its parent. The descendants and parents are mixed in an array and go randomly. The task as in PHP and / or JS to get from this:

Array( Array ( [ID] => 4 [PARENT_ID] => 0 ) Array ( [ID] => 8 [PARENT_ID] => 4) Array ( [ID] => 9 [PARENT_ID] => 3) Array ( [ID] => 6 [PARENT_ID] => 4) Array ( [ID] => 3 [PARENT_ID] => 0 ) Array ( [ID] => 2 [PARENT_ID] => 0 ) ) 

THIS:

  <ul id="ID-4" data-parent="4"> <li id="ID-8" data-child="4"></li> <li id="ID-6" data-child="4"></li> </ul> <ul id="ID-3" data-parent="3"> <li id="ID-8" data-child="3"></li> </ul> <ul id="ID-2" data-parent="2"> </ul> 

The answer I got this. This is a working version. If anyone needs, use.

  $db = array("Наш массив с подмассивами"); foreach($db as $key => $ar) { if(!$ar["PARENT_ID"]){ echo "<ul id='ID-".$ar["ID"]."' data-parent='".$ar["ID"]."' style='background:#aaa;'>".$ar["NAME"]; $db2 = array("Ρ‚ΠΎΡ‚ ΠΆΠ΅ самый массив с подмассивами Π² Π½ΠΎΠ²ΠΎΠΉ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ"); foreach($db2 as $key2 => $ar2) { if($ar2["PARENT_ID"]==$ar["ID"]){ echo "<li id='ID-".$ar2["ID"]."' data-child='".$ar2["PARENT_ID"]."' style='background:#afa;margin:20px auto;'>".$ar2["NAME"]."</li>"; } }echo "</ul>";} } 

    3 answers 3

    Dude, I don't know which one of you is cooler. I'm in no boom boom in PHP. The first option is almost what you need. But I didn’t manage to apply it in reality, because I don’t know how to push new fields with text there. For example, each subarray receives [TEXT] => ["text"] and many fields, like NAME and others

    FORGOT TO ADD important thing! The array is not formed with the numbers given in the example. ID and PARENT_ID may change over time. Naturally, you need to write such code that will not require rework every time you change the elements of the array.

    The second option only saw and be sure to try.

    I personally managed to express in 15 minutes what I need in this way:

      $db = array("Наш массив с подмассивами"); foreach($db as $key => $ar) { if(!$ar["PARENT_ID"]){ echo "<ul style='background:#aaa;'>".$ar["NAME"]; $db2 = array("Ρ‚ΠΎΡ‚ ΠΆΠ΅ самый массив с подмассивами Π² Π½ΠΎΠ²ΠΎΠΉ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ"); foreach($db2 as $key2 => $ar2) { if($ar2["PARENT_ID"]==$ar["ID"]){ echo "<li style='background:#afa;margin:20px auto;'>".$ar2["NAME"]."</li>"; } }echo "</ul>";} } 

    This code may not be as cool as yours, but it does what is necessary and is very understandable. In my particular case, this code is intended for bitrix, and in this case it is necessary to deduce parents as SELECT and descendants as OPTION. Unfortunately, in bitrix when unloading an array, it has all descendants at the same level as the parents. Because of this, they cannot be shoved as an OPTION or even UL> LI

      PHP:

       $array = Array( Array('ID' => 4, 'PARENT_ID' => 0), Array('ID' => 8, 'PARENT_ID' => 4), Array('ID' => 9, 'PARENT_ID' => 3), Array('ID' => 6, 'PARENT_ID' => 4), Array('ID' => 3, 'PARENT_ID' => 0), Array('ID' => 2, 'PARENT_ID' => 0) ); $arr = Array(); for($i = 0; $i < count($array); $i++){ if(empty($arr[$array[$i]['PARENT_ID']])){ $arr[$array[$i]['PARENT_ID']] = Array(); }; }; for($i = 0; $i < count($array); $i++){ array_push($arr[$array[$i]['PARENT_ID']], $array[$i]['ID']); }; for($i = 0; $i < count($arr); $i++){ echo '<ul id="ID-'.array_keys($arr)[$i].'" data-parent="'.array_keys($arr)[$i].'">'; for($a = 0; $a < count($arr[array_keys($arr)[$i]]); $a++){ echo '<li id="ID-'.$arr[array_keys($arr)[$i]][$a].'" data-child="'.array_keys($arr)[$i].'"></li>'; }; echo '</ul>'; }; 

      JQUERY:

       $(function() { var arr = [{id: 4, parent_id: 0}, {id: 8, parent_id: 4}, {id: 9, parent_id: 3}, {id: 6, parent_id: 4}, {id: 3, parent_id: 0}, {id: 2, parent_id: 0}] for(var i = 0; i < arr.length; i++){ if($('ul#ID-'+arr[i].parent_id).length !== 0){ $('ul#ID-'+arr[i].parent_id).append('<li id="ID-'+arr[i].id+'" data-child="'+arr[i].parent_id+'"></li>'); }else{ $('body').append('<ul id="ID-'+arr[i].parent_id+'" data-parent="'+arr[i].parent_id+'"><li id="ID-'+arr[i].id+'" data-child="'+arr[i].parent_id+'"></li></ul>'); }; }; }); 

        If you just arrange the array for further output without calculations, you can do this:

         $struct = []; $arr = [ ['ID' => 4 , 'PARENT_ID' => 0] , ['ID' => 5 , 'PARENT_ID' => 4] , ['ID' => 9 , 'PARENT_ID' => 3] , ['ID' => 6 , 'PARENT_ID' => 4] , ['ID' => 3 , 'PARENT_ID' => 0] , ['ID' => 2 , 'PARENT_ID' => 0] , ]; array_map(function($el)use(&$struct , $arr){ $parent = $el['PARENT_ID']; if(!$struct[$parent]){ $struct[$parent] = array_filter($arr , function($el)use($parent){ return $el['PARENT_ID']==$parent; }); } } , $arr); print_r($struct); 

        at the output, we obtain an array grouped by PARENT_ID :

          [0] => Array ( [0] => Array ( [ID] => 4 [PARENT_ID] => 0 ) [4] => Array ( [ID] => 3 [PARENT_ID] => 0 ) [5] => Array ( [ID] => 2 [PARENT_ID] => 0 ) )...