The array has the form:

Array ( [1] => Array ( [id] => 1 [title] => Гардероб [parent_id] => 0 [childs] => Array ( [3] => Array ( [id] => 3 [title] => Мужской [parent_id] => 1 [childs] => Array ( [5] => Array ( [id] => 5 [title] => Одежда [parent_id] => 3 [childs] => Array ( [11] => Array ( [id] => 11 [title] => Куртки [parent_id] => 5 ) ) ) [6] => Array ( [id] => 6 [title] => Обувь [parent_id] => 3 ) [7] => Array ( [id] => 7 [title] => Аксессуары [parent_id] => 3 ) ) ) [4] => Array ( [id] => 4 [title] => Женский [parent_id] => 1 [childs] => Array ( [8] => Array ( [id] => 8 [title] => Одежда [parent_id] => 4 [childs] => Array ( [12] => Array ( [id] => 12 [title] => Куртки [parent_id] => 8 ) ) ) [9] => Array ( [id] => 9 [title] => Обувь [parent_id] => 4 ) [10] => Array ( [id] => 10 [title] => Аксессуары [parent_id] => 4 ) ) ) ) ) [2] => Array ( [id] => 2 [title] => Электроника [parent_id] => 0 [childs] => Array ( [13] => Array ( [id] => 13 [title] => Ноутбуки [parent_id] => 2 ) [14] => Array ( [id] => 14 [title] => Смартфоны [parent_id] => 2 ) [16] => Array ( [id] => 16 [title] => Планшеты [parent_id] => 2 ) [17] => Array ( [id] => 17 [title] => Беспроводные АС [parent_id] => 2 ) [18] => Array ( [id] => 18 [title] => Умные часы и браслеты [parent_id] => 2 ) ) ) [15] => Array ( [id] => 15 [title] => Книги [parent_id] => 0 ) ) 

You need to get the nesting level of each element of the array. It is necessary to build a "ladder" - set the indent for each menu item, depending on the level of nesting. The array is displayed in <select> so that one css does not get rid of here.

  • Are you now somehow output this array? Recursively? - cheops
  • @cheops yes, I deduce. But it is displayed in the usual list without indents. you need to get at output the level of nesting of a particular item and add the corresponding padding or the number of spaces to it; nbsp; - Torawhite

2 answers 2

You can add an additional parameter $ offset to the recursive function that will indent and increase with each recursive function call

 <?php $arr = Array( Array( 'id' => 1, 'title' => 'Гардероб', 'parent_id' => '0', 'childs' => Array( Array( 'id' => 3, 'title' => 'Мужской', 'parent_id' => 1, 'childs' => Array( Array( 'id' => 5, 'title' => 'Одежда', 'parent_id' => 3, 'childs' => Array( Array( 'id' => 11, 'title' => 'Куртки', 'parent_id' => 5 ) ) ), Array( 'id' => 6, 'title' => 'Обувь', 'parent_id' => 3 ) ) ) ) ) ); function select($arr, $offset = '') { foreach($arr as $val) { echo "<option name='{$val['id']}'>{$offset}". htmlspecialchars($val['title'])."</option>"; if(!empty($val['childs'])) select($val['childs'], $offset . '&nbsp;&nbsp;&nbsp;&nbsp;'); } } echo "<select name='hierarchy'>"; echo select($arr); echo "</select>"; 
  • Your option works exactly as needed. Thank you very much! - Torawhite

In my opinion, only recursion.

Something like this:

 function buildResult($level, $myarray_arr) { if (empty($myarray)) return false; foreach($myarray_arr as $myarray) { $result[] = array('id'=>$myarray['id'], 'title'=>$myarray['title'], 'parent_id'=>$myarray['parent_id'], 'level'=>$level, 'childs'=>buildResult($level+1, $myarray['childs'])); } return $result; } $show_arr = buildResult(0, $myarray_arr); 

You will now have $ level, which you can consider in the output.

  • that is, your function creates another element with a level value? return not needed, or you just forgot to specify? - Torawhite
  • You are right, you have to be a retour, and you have found 2 more typos of your own: you must add an element in the result, and not just assign it; we need an element in which all our recursive buildResult () will initially be collected ... I apologize, corrected. In fact, yes, just added another element with a level value. - Stanislav
  • does not work, the function returns nothing. Doesn't even give an empty array ( - Torawhite
  • I am powerless in front of you ... you didn’t even think about how the function works, that you need to transfer your minimal array to it ... this is trash ((( - Stanislav