title: Flatten array while preserving key value pairs in PHP
body: <p>I have an array like this:</p>
  
  <pre><code>array (size=4)
    0 =&gt;
      array (size=4)
        key =&gt; value
        key =&gt; value
        key =&gt; value
        key =&gt; value
    1 =&gt;
      array (size=2)
        key =&gt; value
        key =&gt; value
    2 =&gt;
      array (size=1)
        key =&gt; value
    3 =&gt;
      array (size=1)
        key =&gt; value
  </code></pre>
  
  <p>I want to flatten the array to be this:</p>
  
  <pre><code>array (size=4)
    key =&gt; value
    key =&gt; value
    key =&gt; value
    key =&gt; value
    key =&gt; value
    key =&gt; value
    key =&gt; value
    key =&gt; value
  </code></pre>
  
  <p>I have tried my own solutions using things like <code>array_merge</code>, <code>array_walk_recursive</code>, and <code>RecursiveIteratorIterator</code> with <code>RecursiveArrayIterator</code>. I also tried many of the solutions posted on similar questions on StackOverflow, but none of them work the way I would expect. They either do not preserve the key value pairs, or they give me an array that is identical to the original. My attempts look something like this:</p>
  
  <pre><code>$multidimensionalArray = array(stuff goes here);
  $flatArray = array();
  
  function flattenArray ($array) {
    foreach ($array as $key =&gt; $value) {
      if (is_array($value) {
        flattenArray($value);
      }
      else {
        $flatArray[$key] = $value;
      }
    }
  }
  
  flattenArray($multidimensionalArray);
  </code></pre>