title: Replace multidimensional rows with key-value pairs from each of its leafnodes
body: <p>I want to flatten an array of row -- each with variable depth -- so that it becomes an array of associative arrays.</p>
  <p>I tried some very good examples over here that works for associative or multi dimensional arrays but couldn't put them together in a good use to solve my problem:</p>
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [level2_1] =&gt; aa
              [level2_2] =&gt; bb
              [level2_3] =&gt; cc
              [level2_4] =&gt; Array
                  (
                      [level3_1] =&gt; DDD
                      [level3_2] =&gt; EEE
                  )
  
          )
  
      [1] =&gt; Array
          (
              [level2_1] =&gt; ff
              [level2_2] =&gt; gg
              [level2_3] =&gt; hh
              [level2_4] =&gt; Array
                  (
                      [level3_1] =&gt; III
                      [level3_2] =&gt; JJJ
                  )
  
          )
  
  )
  </code></pre>
  <p><strong>Expected output:</strong></p>
  <pre><code>0 -&gt; level2_1: aa, level2_2: bb, level2_3: cc, level3_1: DDD, level3_2: EEE
  1 -&gt; level2_1: ff, level2_2: gg, level2_3: hh, level3_1: III, level3_2: JJJ
  </code></pre>
  <p><strong>Or At Least:</strong></p>
  <pre><code>0: aa, bb, cc, DDD, EEE
  1: ff, gg, hh, III, JJJ
  </code></pre>
  <p>I tried many non-loop answers regarding <strong>implode, multi-dimension</strong>, etc.
  I think they need some modification if one of the element (level2_4) is array instead of string, flatten its elements.</p>
  <p>Here are some good samples of code I found working but needs to be modified to fit my case:</p>
  <blockquote>
  <p><strong># Basic but stops at a sub-array:</strong></p>
  <p>echo implode(',', call_user_func_array('array_merge', $data));</p>
  <p><strong># PHP implode associative array:</strong></p>
  <p>implode(', ', array_map(function ($k, $v) { return &quot;$k: $v&quot;; }, array_keys($data), array_values($data)));</p>
  <p><strong># PHP implode multi dimensional array:</strong></p>
  <p>$result[0][code] = &quot;123&quot;;</p>
  <p>$result[0][desc] = &quot;Desc123&quot;;</p>
  <p>$result[1][code] = &quot;567&quot;;</p>
  <p>$result[1][desc] = &quot;Desc567&quot;;</p>
  <p>echo implode(', ', array_map(function ($k, $v) { return &quot;$k: $v&quot;; }, array_keys(array_column($result, 'desc', 'code')), array_values(array_column($result, 'desc', 'code'))));</p>
  </blockquote>