title: How to fix recursion in php?
body: <p>I try to iterate all parents/child in <strong>recursion</strong>:</p>
  
  <pre><code>foreach ($data as $k =&gt; $item) {
     $data[$k]["children"] = $this-&gt;getChildren($all, $item);
  }
  </code></pre>
  
  <p>And <strong>recursive function</strong>:</p>
  
  <pre><code> private function getChildren($list, $item)
      {
          $children = [];
  
          if (is_array($item["children"])) {
  
              foreach ($item["children"] as $k =&gt; $id) {
                  $children[] = $list[$id];
              }
  
          } else {
  
              if (isset($list[$item["children"]])) {
                  $children[] = $list[$item["children"]];
              }
          }
  
          return $children;
      }
  </code></pre>
  
  <p>Problem is that I need to call <code>getChildren</code> inside <code>getChildren</code> for founded element <code>$list[$id]</code> to check if this element does contain array <strong>children</strong>.</p>
  
  <p>I tried to call this:</p>
  
  <pre><code>$children[] = $this-&gt;getChildren($list, $list[$id]);
  </code></pre>
  
  <p>But it does not work.</p>
  
  <p>I tried this way:</p>
  
  <pre><code>private function getChildren($list, $item)
      {
          $children = [];
  
          if (is_array($item["children"]) &amp;&amp; count($item["children"]) &gt; 0) {
  
              foreach ($item["children"] as $k =&gt; $id) {
                  return $this-&gt;getChildren($list, $list[$id]);
              }
          }
  
          return $list[$id];
      }
  </code></pre>
  
  <p>The array <code>$all</code> that I try to iterate by <code>children</code>:</p>
  
  <pre><code>array:12 [
    "592d0b3bb3569e257208db81" =&gt; array:10 [
      "_id" =&gt; MongoId {#266
        +$id: "592d0b3bb3569e257208db81"
      }
      "name" =&gt; "Характеристики героя"
      "default" =&gt; "data"
      "visibility" =&gt; null
      "type" =&gt; "2"
      "value" =&gt; "data"
      "prefix" =&gt; "data"
      "type_value" =&gt; "object"
      "children" =&gt; array:7 [
        0 =&gt; "592d094eb3569eb07208db7e"
        1 =&gt; "592d0961b3569ee17708db80"
        2 =&gt; "592d0978b3569eb07108db7e"
        3 =&gt; "592d0987b3569ead7108db91"
        4 =&gt; "592d09a5b3569eae7108db81"
        5 =&gt; "592d09c1b3569eae7208db82"
        6 =&gt; "592d0ac3b3569eae7208db83"
      ]
      "available" =&gt; "1"
    ]
    "592d0ac3b3569eae7208db83" =&gt; array:10 [
      "_id" =&gt; MongoId {#267
        +$id: "592d0ac3b3569eae7208db83"
      }
      "name" =&gt; "Умения"
      "default" =&gt; "skills"
      "visibility" =&gt; null
      "type" =&gt; "2"
      "value" =&gt; "skills"
      "prefix" =&gt; "skills"
      "type_value" =&gt; "object"
      "children" =&gt; array:4 [
        0 =&gt; "592d094eb3569eb07208db7e"
        1 =&gt; "592d0a1db3569ee17708db81"
        2 =&gt; "592d0a2fb3569e257208db80"
        3 =&gt; "592d0a58b3569ead7108db92"
      ]
      "available" =&gt; "1"
    ]
    "592d0a58b3569ead7108db92" =&gt; array:10 [
      "_id" =&gt; MongoId {#268
        +$id: "592d0a58b3569ead7108db92"
      }
      "name" =&gt; "Начальная прокачка"
      "default" =&gt; "0"
      "visibility" =&gt; null
      "type" =&gt; "1"
      "value" =&gt; "0"
      "prefix" =&gt; "startPumping"
      "type_value" =&gt; "integer"
      "children" =&gt; []
      "available" =&gt; "1"
    ]
    "592d0a2fb3569e257208db80" =&gt; array:10 [
      "_id" =&gt; MongoId {#269
        +$id: "592d0a2fb3569e257208db80"
      }
      "name" =&gt; "Максимальная прокачка"
      "default" =&gt; "0"
      "visibility" =&gt; null
      "type" =&gt; "1"
      "value" =&gt; "0"
      "prefix" =&gt; "maxPumping"
      "type_value" =&gt; "integer"
      "children" =&gt; []
      "available" =&gt; "1"
    ]
    "592d0a1db3569ee17708db81" =&gt; array:10 [
      "_id" =&gt; MongoId {#270
        +$id: "592d0a1db3569ee17708db81"
      }
      "name" =&gt; "Текущая прокачка"
      "default" =&gt; "0"
      "visibility" =&gt; null
      "type" =&gt; "1"
      "value" =&gt; "0"
      "prefix" =&gt; "pumping"
      "type_value" =&gt; "integer"
      "children" =&gt; []
      "available" =&gt; "1"
    ]
    "592d09c1b3569eae7208db82" =&gt; array:10 [
      "_id" =&gt; MongoId {#271
        +$id: "592d09c1b3569eae7208db82"
      }
      "name" =&gt; "Свободных умений"
      "default" =&gt; "0"
      "visibility" =&gt; null
      "type" =&gt; "1"
      "value" =&gt; "0"
      "prefix" =&gt; "freePumpingPoints"
      "type_value" =&gt; "integer"
      "children" =&gt; []
      "available" =&gt; "1"
    ]
    "592d09a5b3569eae7108db81" =&gt; array:10 [
      "_id" =&gt; MongoId {#272
        +$id: "592d09a5b3569eae7108db81"
      }
      "name" =&gt; "Требования"
      "default" =&gt; "Что бы получить Этого персонажа, вам нужно заплатить миллион американских рублей и подарить нам почку"
      "visibility" =&gt; null
      "type" =&gt; "1"
      "value" =&gt; "Что бы получить Этого персонажа, вам нужно заплатить миллион американских рублей и подарить нам почку"
      "prefix" =&gt; "requirements"
      "type_value" =&gt; "string"
      "children" =&gt; []
      "available" =&gt; "1"
    ]
    "592d0987b3569ead7108db91" =&gt; array:10 [
      "_id" =&gt; MongoId {#273
        +$id: "592d0987b3569ead7108db91"
      }
      "name" =&gt; "Описание"
      "default" =&gt; "Описание"
      "visibility" =&gt; null
      "type" =&gt; "1"
      "value" =&gt; "Описание"
      "prefix" =&gt; "description"
      "type_value" =&gt; "string"
      "children" =&gt; []
      "available" =&gt; "1"
    ]
    "592d0978b3569eb07108db7e" =&gt; array:10 [
      "_id" =&gt; MongoId {#274
        +$id: "592d0978b3569eb07108db7e"
      }
      "name" =&gt; "Название"
      "default" =&gt; "Название"
      "visibility" =&gt; null
      "type" =&gt; "1"
      "value" =&gt; "Название"
      "prefix" =&gt; "name"
      "type_value" =&gt; "string"
      "children" =&gt; []
      "available" =&gt; "1"
    ]
    "592d0961b3569ee17708db80" =&gt; array:10 [
      "_id" =&gt; MongoId {#275
        +$id: "592d0961b3569ee17708db80"
      }
      "name" =&gt; "Уровень"
      "default" =&gt; "0"
      "visibility" =&gt; null
      "type" =&gt; "1"
      "value" =&gt; "0"
      "prefix" =&gt; "level"
      "type_value" =&gt; "integer"
      "children" =&gt; []
      "available" =&gt; "1"
    ]
    "592d094eb3569eb07208db7e" =&gt; array:10 [
      "_id" =&gt; MongoId {#276
        +$id: "592d094eb3569eb07208db7e"
      }
      "name" =&gt; "Идентификатор"
      "default" =&gt; "0"
      "visibility" =&gt; null
      "type" =&gt; "1"
      "value" =&gt; "0"
      "prefix" =&gt; "id"
      "type_value" =&gt; "integer"
      "children" =&gt; []
      "available" =&gt; "1"
    ]
    "592d0932b3569e277208db87" =&gt; array:10 [
      "_id" =&gt; MongoId {#277
        +$id: "592d0932b3569e277208db87"
      }
      "name" =&gt; "Сила отбития"
      "default" =&gt; "0"
      "visibility" =&gt; null
      "type" =&gt; "1"
      "value" =&gt; "0"
      "prefix" =&gt; "hitForce"
      "type_value" =&gt; "integer"
      "children" =&gt; []
      "available" =&gt; "1"
    ]
  ]"available" =&gt; "1"
    ]
  ]
  </code></pre>
  
  <p>My version of recursie:</p>
  
  <pre><code>private function getChildren($list, $item, $c)
      {
  
          if (is_array($item["children"])) {
  
              foreach ($item["children"] as $k =&gt; $id) {
  
  
                  if(count($list[$id]["children"]) &gt; 0){
  
                      $_id = (string)$list[$id]["_id"];
  
                      $c[][$_id] = $list[$id];
                      $c[][$_id]["children"] = $this-&gt;getChildren($list, $list[$id], $c);
  
                  } else {
  
                      $c[] = $list[$id];
                  }
              }
          }
  
          return $c;
      }
  </code></pre>
  
  <p><strong>Calling:</strong></p>
  
  <pre><code> foreach ($data as $k =&gt; $item) {
  
              $data[$k]["children"] = $this-&gt;getChildren($all, $item, $c);
          }
  </code></pre>