title: PHP recursive array from JSON to key-value to values
body: <p>I want to apologize in advanced if this was already asked, I'm not exactly sure what this would be called.</p>
  
  <hr>
  
  <p>I'm storing data from a form into a MongoDB database and I'd like to create defined key-value pairs to make sorting easier.</p>
  
  <p>Using this code I am able to do this with a one-dimensional array, but it does not work with multidimensional arrays:</p>
  
  <pre><code>/* $array = The array */
  $new_array = array();
  
  foreach ($array as $key =&gt; $value) {
      array_push($new_array, array(
          'name' =&gt; $key,
          'value' =&gt; $value
      ));
  }
  </code></pre>
  
  <p><strong>Example:</strong></p>
  
  <p>Input array:</p>
  
  <pre><code>Array
  (
      [email] =&gt; test@mail.com
      [name] =&gt; John
      [sports] =&gt; Array
          (
              [outdoor] =&gt; Array
                  (
                      [0] =&gt; Football
                      [1] =&gt; Baseball
                  )
  
              [indoor] =&gt; Array
                  (
                      [0] =&gt; Basketball
                      [1] =&gt; Hockey
                  )
          )
  )
  </code></pre>
  
  <p>Output array:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [name] =&gt; email
              [value] =&gt; test@mail.com
          )
  
      [1] =&gt; Array
          (
              [name] =&gt; name
              [value] =&gt; John
          )
  
      [2] =&gt; Array
          (
              [name] =&gt; sports
              [value] =&gt; Array
                  (
                      [outdoor] =&gt; Array
                          (
                              [0] =&gt; Football
                              [1] =&gt; Baseball
                          )
  
                      [indoor] =&gt; Array
                          (
                              [0] =&gt; Basketball
                              [1] =&gt; Hockey
                          )
                  )
          )
  )
  </code></pre>
  
  <p>Notice how it stops at the sports value array and does not change the array within it.  How can I continue this pattern throughout all the arrays within it?</p>