title: PHP - Create a single array from a multidimensional array based on key name
body: <p>I know there are a lot of answers on multi-dimensional arrays but I couldn't find what I was looking for exactly. I'm new to PHP and can't quite get my head around some of the other examples to modify them. If someone could show me the way, it would be much appreciated.</p>
  
  <p>An external service is passing me the following multidimensional array.</p>
  
  <pre><code>$mArray = Array (
    [success] =&gt; 1
    [errors] =&gt; 0
    [data] =&gt; Array (
      [0] =&gt; Array (
        [email] =&gt; me@example.com
        [id] =&gt; 123456 
        [email_type] =&gt; html 
        [ip_opt] =&gt; 10.10.1.1 
        [ip_signup] =&gt; 
        [member_rating] =&gt; X 
        [info_changed] =&gt; 2011-08-17 08:56:51 
        [web_id] =&gt; 123456789
        [language] =&gt;
        [merges] =&gt; Array (
          [EMAIL] =&gt; me@example.com
          [NAME] =&gt; Firstname 
          [LNAME] =&gt; Lastname 
          [ACCOUNT] =&gt; ACME Ltd 
          [ACCMANID] =&gt; 123456adc 
          [ACCMANTEL] =&gt; 1234 123456 
          [ACCMANMAIL] =&gt; an.other@example.com
          [ACCMANFN] =&gt; Humpty
          [ACCMANLN] =&gt; Dumpty 
        )
        [status] =&gt; unknown
        [timestamp] =&gt; 2011-08-17 08:56:51
        [lists] =&gt; Array ( ) 
        [geo] =&gt; Array ( ) 
        [clients] =&gt; Array ( ) 
        [static_segments] =&gt; Array ( )
      ) 
    ) 
  )
  </code></pre>
  
  <p>The only information I'm interested in are the key/value pairs that are held in the array under the key name 'merges'. It's about the third array deep. The key name of the array will always be called merges but there's no guarantee that its location in the array won't be moved. The number of key/value pairs in the merges array is also changeable.</p>
  
  <p>I think what I need is a function for array_walk_recursive($mArray, "myfunction", $search);, where $search holds the string for the Key name (merges) I'm looking for. It needs to walk the array until it finds the key, check that it holds an array and then (preserving the keys), return each key/value pair into a single array.</p>
  
  <p>So, for clarity, the output of the function would return:</p>
  
  <pre><code>    $sArray = Array (
        [EMAIL] =&gt; me@example.com
        [NAME] =&gt; Firstname 
        [LNAME] =&gt; Lastname 
        [ACCOUNT] =&gt; ACME Ltd 
        [ACCMANID] =&gt; 123456adc 
        [ACCMANTEL] =&gt; 1234 123456 
        [ACCMANMAIL] =&gt; an.other@example.com 
        [ACCMANFN] =&gt; Humpty 
        [ACCMANLN] =&gt; Dumpty
      )
  </code></pre>
  
  <p>I can then move on to the next step in my project, which is to compare the keys in the single merges array to element IDs obtained from an HTML DOM Parser and replace the  attribute values with those contained in the single array.</p>
  
  <p>I probably need a foreach loop. I know I can use is_array to verify if $search is an array. It's joining it all together that I'm struggling with.</p>
  
  <p>Thanks for your help.</p>