title: PHP Laravel recursive function with nested array
body: <p>I am using <code>Laravel 4</code> with <code>MySQL</code> back-end. </p>
  
  <p>I want to store records of a <code>nested array</code> into database using <code>recursive function</code>. </p>
  
  <p>The input array is as below : </p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
      (
          'id' =&gt; 561,
          'type' =&gt; 'q',
          'subtype' =&gt; 'boolean',
          'title' =&gt; 'Did you..?',
          'parent' =&gt; 560,
          'created_at' =&gt; '2014-07-09 09:45:50',
          'updated_at' =&gt; NULL,
          'deleted_at' =&gt; NULL,
          'children' =&gt; Array
              (
                  [0] =&gt; Array
                      (
                          'id' =&gt; 562,
                          'type' =&gt; 'o',
                          'subtype' =&gt; NULL,
                          'title' =&gt; 'Yes',
                          'parent' =&gt; 561,
                          'created_at' =&gt; '2014-07-09 09:45:50',
                          'updated_at' =&gt; NULL,
                          'deleted_at' =&gt; NULL,
                          'children' =&gt; Array
                              (
                              )
                      )
                  [1] =&gt; Array
                      (
                          'id' =&gt; 563,
                          'type' =&gt; 'answer',
                          'subtype' =&gt; NULL,
                          'title' =&gt; 'No',
                          'parent' =&gt; 561,
                          'created_at' =&gt; '2014-07-09 09:45:50',
                          'updated_at' =&gt; 'NULL',
                          'deleted_at' =&gt; NULL,
                          'children' =&gt; Array
                              (
                              )
                      )
              )
      )
  )
  </code></pre>
  
  <p>The <code>recursive function</code> I am using store the records into the database is as below : </p>
  
  <pre><code>public function addRecursiveChildren(array $surveychildren, $parentid, $userid){
  
      foreach ($surveychildren as $item) 
      {
          /* Error : HTTPRequest Error :: 500: {"error":{"type":"ErrorException","message":"Cannot use a scalar value as an array
             Error is in the statement below in the second recursion when child is passes as an input.
          */
          $item['survey_id'] = $item['id']; 
          $item['user_id'] = $userid;
          $item['id'] = null; 
          $item['parent'] = $parentid; 
  
          $routine = routine::create($item);
  
          if(count($item["children"]) &gt; 0)
          {
              foreach ($item["children"] as $child) 
              {
                  /* The $child I found is as below : 
                  $child = array(
                      'id' =&gt; 562,
                      'type' =&gt; 'o',
                      'subtype' =&gt; NULL ,
                      'title' =&gt; 'Yes',
                      'parent' =&gt; 561,
                      'created_at' =&gt; '2014-07-09 09:45:50',
                      'updated_at' =&gt; NULL,
                      'deleted_at' =&gt; NULL,
                      'children' =&gt; Array
                          (
                          )
                  );
                  */
  
                  RoutineRepository::addRecursiveChildren($child, $routine-&gt;id, $userid);
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Edit :</strong> </p>
  
  <p>I know that cause of error is the <code>$child</code> I am passing as an <code>input array</code> to the <code>recursive function</code> above :</p>
  
  <p>The <code>$child</code> is something like this : </p>
  
  <pre><code>array(
      'id' =&gt; 562,
      'type' =&gt; 'o',
      'subtype' =&gt; NULL ,
      'title' =&gt; 'Yes',
      'parent' =&gt; 561,
      'created_at' =&gt; '2014-07-09 09:45:50',
      'updated_at' =&gt; NULL,
      'deleted_at' =&gt; NULL,
      'children' =&gt; Array
      (
      )
  )
  </code></pre>
  
  <p>Instead of this if <code>$child</code> will be something like this : </p>
  
  <pre><code>Array
  (
      [0] =&gt;
      array(
          'id' =&gt; 562,
          'type' =&gt; 'o',
          'subtype' =&gt; NULL ,
          'title' =&gt; 'Yes',
          'parent' =&gt; 561,
          'created_at' =&gt; '2014-07-09 09:45:50',
          'updated_at' =&gt; NULL,
          'deleted_at' =&gt; NULL,
          'children' =&gt; Array
          (
          )
      )
  )
  </code></pre>
  
  <p>Then there will be no error. </p>
  
  <p>Can anybody help me to overcome it?</p>
  
  <p>Thanks.</p>