N
N
neeil2018-03-01 20:07:36
PHP
neeil, 2018-03-01 20:07:36

PHP Nest associative arrays inside each other?

Hello everyone, I have an array

array(15) {
  [0]=>
  array(4) {
    ["DEPTH_LEVEL"]=>
    string(1) "1"
    ["IS_PARENT"]=>
    bool(true)
    ["NAME"]=>
    string(10) "Обувь"
    ["LINK"]=>
    string(15) "/catalog/shoes/"
  }
  [1]=>
  array(4) {
    ["DEPTH_LEVEL"]=>
    string(1) "2"
    ["IS_PARENT"]=>
    bool(false)
    ["NAME"]=>
    string(18) "Пантолеты"
    ["LINK"]=>
    string(19) "/catalog/pantolety/"
  }
  [2]=>
  array(4) {
    ["DEPTH_LEVEL"]=>
    string(1) "2"
    ["IS_PARENT"]=>
    bool(false)
    ["NAME"]=>
    string(14) "Тапочки"
    ["LINK"]=>
    string(18) "/catalog/slippers/"
  }
  [3]=>
  array(4) {
    ["DEPTH_LEVEL"]=>
    string(1) "2"
    ["IS_PARENT"]=>
    bool(true)
    ["NAME"]=>
    string(10) "Туфли"
    ["LINK"]=>
    string(16) "/catalog/shoess/"
  }
  [4]=>
  array(4) {
    ["DEPTH_LEVEL"]=>
    string(1) "3"
    ["IS_PARENT"]=>
    bool(false)
    ["NAME"]=>
    string(26) " Туфли Женские"
    ["LINK"]=>
    string(23) "/catalog/women-s-shoes/"
  }
  [5]=>
  array(4) {
    ["DEPTH_LEVEL"]=>
    string(1) "3"
    ["IS_PARENT"]=>
    bool(false)
    ["NAME"]=>
    string(25) "Туфли Мужские"
    ["LINK"]=>
    string(20) "/catalog/shoes-mens/"
  }
  [6]=>
  array(4) {
    ["DEPTH_LEVEL"]=>
    string(1) "1"
    ["IS_PARENT"]=>
    bool(false)
    ["NAME"]=>
    string(12) "Платья"
    ["LINK"]=>
    string(17) "/catalog/dresses/"
  }
  [7]=>
  array(4) {
    ["DEPTH_LEVEL"]=>
    string(1) "1"
    ["IS_PARENT"]=>
    bool(false)
    ["NAME"]=>
    string(10) "Штаны"
    ["LINK"]=>
    string(15) "/catalog/pants/"
  }
  [8]=>
  array(4) {
    ["DEPTH_LEVEL"]=>
    string(1) "1"
    ["IS_PARENT"]=>
    bool(false)
    ["NAME"]=>
    string(23) "Нижнее белье"
    ["LINK"]=>
    string(19) "/catalog/underwear/"
  }
  [9]=>
  array(4) {
    ["DEPTH_LEVEL"]=>
    string(1) "1"
    ["IS_PARENT"]=>
    bool(false)
    ["NAME"]=>
    string(16) "Футболки"
    ["LINK"]=>
    string(18) "/catalog/t-shirts/"
  }
  [10]=>
  array(4) {
    ["DEPTH_LEVEL"]=>
    string(1) "1"
    ["IS_PARENT"]=>
    bool(false)
    ["NAME"]=>
    string(101) "Спортивная Одежда Спортивная Одежда Спортивная Одежда"
    ["LINK"]=>
    string(20) "/catalog/sportswear/"
  }
  [11]=>
  array(4) {
    ["DEPTH_LEVEL"]=>
    string(1) "1"
    ["IS_PARENT"]=>
    bool(true)
    ["NAME"]=>
    string(20) "Аксессуары"
    ["LINK"]=>
    string(21) "/catalog/accessories/"
  }
  [12]=>
  array(4) {
    ["DEPTH_LEVEL"]=>
    string(1) "2"
    ["IS_PARENT"]=>
    bool(true)
    ["NAME"]=>
    string(10) "Ремни"
    ["LINK"]=>
    string(15) "/catalog/belts/"
  }
  [13]=>
  array(4) {
    ["DEPTH_LEVEL"]=>
    string(1) "3"
    ["IS_PARENT"]=>
    bool(false)
    ["NAME"]=>
    string(25) "Ремни Женские"
    ["LINK"]=>
    string(23) "/catalog/women-s-belts/"
  }
  [14]=>
  array(4) {
    ["DEPTH_LEVEL"]=>
    string(1) "3"
    ["IS_PARENT"]=>
    bool(false)
    ["NAME"]=>
    string(25) "Ремни Мужские"
    ["LINK"]=>
    string(21) "/catalog/men-s-belts/"
  }
}

elements have a nesting level DEPTH_LEVEL
and a sign of a parent IS_PARENT
how to add arrays into each other given DEPTH_LEVEL one by one?
I know that you can make a recursive function to get a directory tree, but for this you need to know the id of the parent.
I can't believe there is no elegant solution to do this without Id_parent.
Thanks everyone.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Boris Korobkov, 2018-03-02
@BorisKorobkov

Trees are stored differently. Usually parent_id. Sometimes left-right (nested sets). Sometimes some other formats.
Manually bring your structure to the correct form.

A
Alexander Lykasov, 2018-03-02
@lykasov-aleksandr

Here is a description of the main ways to store trees: https://habrahabr.ru/post/46659/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question