title: PHP foreach array - keep keys
body: <p>I have a JSON file which structure looks like this (very simplified):</p>
  <pre><code>    [
        {
          &quot;customerId&quot;: &quot;M12345&quot;,
          &quot;houses&quot;: [
            {
               &quot;id&quot;: &quot;OBJ12345_1731321200&quot;,
               &quot;status&quot;: {
                 &quot;id&quot;: &quot;4&quot;,
                &quot;name&quot;: &quot;Sold&quot;
              },
             {
              &quot;id&quot;: &quot;OBJ12345_1732393235&quot;,
              &quot;status&quot;: {
                &quot;id&quot;: &quot;4&quot;,
                &quot;name&quot;: &quot;Såld&quot;
              }
            ],
           &quot;plots&quot;: [
            {
              &quot;id&quot;: &quot;OBJ12345_1771637082&quot;,
              &quot;status&quot;: {
                &quot;id&quot;: &quot;4&quot;,
                &quot;name&quot;: &quot;Sold&quot;
              }
             ],
          &quot;projects&quot;: [],
          &quot;farms&quot;: [],
          &quot;commercialPropertys&quot;: [],
          &quot;condominiums&quot;: [],
          &quot;foreignProperties&quot;: [],
          &quot;premises&quot;: []
         }
      ]
  </code></pre>
  <p>I made this to get an array of all object but have no idea how to keep the keys &quot;houses&quot; or &quot;plots&quot;?</p>
  <pre><code>    // Create array of all objects  
        $AllEstatesList = array();
          foreach ($GetEstateList[0] as $GetEstateType) { 
              foreach ($GetEstateType as $GetEstate) { 
                  if ($GetEstate[&quot;id&quot;] != null) {
                      $AllEstatesList[] = $GetEstate;
                  }
              }
          }
  </code></pre>
  <p>I need to figure out how to create an array that keeps the keys from parent and includes them in the new array...</p>
  <p>The result I'm looking for is this:</p>
  <pre><code>    Array
          (
              [0] =&gt; Array
                  (
                      [type] =&gt; houses
                      [id] =&gt; OBJ12345_1731321200
                      [status] =&gt; Array
                          (
                              [id] =&gt; 4
                              [name] =&gt; Såld
                          )
  
          
                  )
  
              [1] =&gt; Array
                  (
                      [type] =&gt; houses
                      [id] =&gt; OBJ12345_1732393235
                      [status] =&gt; Array
                          (
                              [id] =&gt; 4
                              [name] =&gt; Såld
                          )
                  )
  
              [2] =&gt; Array
                  (
                      [type] =&gt; plots
                      [id] =&gt; OBJ15053_1771637082
                      [status] =&gt; Array
                          (
                              [id] =&gt; 4
                              [name] =&gt; Såld
                          )
  
                  )
  
          )
  </code></pre>
  <p>please help! :)</p>