title: How to make a multi dimensional array in a foreach loop
body: <p>I'm trying to create a multi dimensional array in a foreach loop.</p>
  
  <p>First let me place my code:</p>
  
  <pre><code>$jsonstring = [];
  
  
  foreach($results as $result){
  
      if(isset($simplicate_fields['data'][0]['custom_fields'][17]['value'])){
              $dexcode_value          =   $simplicate_fields['data'][0]['custom_fields'][17]['value'];
      }else{
            $dexcode_value          = '';
      }
  
  
      if(isset($simplicate_fields['data'][0]['visiting_address']['country'])){
              $simplicate_land        =   $simplicate_fields['data'][0]['visiting_address']['country'];
      }else{
           $simplicate_land        = '';
      }
  
  
      $body = array(
  
          'DEXCODE' =&gt; $dexcode_value,
  
          'LAND' =&gt; $simplicate_land,
      );
  
  
      $jsonstring = array_merge($jsonstring , $body);
  
  }
  
  print_r($jsonstring);
  </code></pre>
  
  <p>Now let me explain. Before the loop I create an empty array called jsonstring.
  Inside the loop i create another array called body. The body array contains data from the array im looping through. At the end of the loop I merge body with json string.</p>
  
  <p>The problem is, whenever I print jsonstring outside the loop it only gives me 1 instance of the loop. The results look like this:</p>
  
  <pre><code>(
      [DEXCODE] =&gt; 
      [LAND] =&gt; Country_The Netherlands
      [CP GENDER] =&gt; heer
      [OROrganisation] =&gt; private
      [STRAATNAAM] =&gt; private
      [ORCITY] =&gt; private
      [OREMAIL] =&gt; private
      [ORWEBSITE] =&gt; private
      [CP_PHONE] =&gt; private
      [first_name] =&gt; private
      [last name] =&gt; private
      [FUNCTIE] =&gt; private
      [CPMobile] =&gt; private
      [CPEmail1] =&gt; private
      [ORPostcode] =&gt; private
      [ORTelephone] =&gt; private
      [ACTIEFINDUITSLAND] =&gt; Ja
      [GESPREKSVELDSIMPLICATE] =&gt; private
      [organisatie grootte] =&gt; Onbekend
      [B2B of B2C] =&gt; Onbekend
      [doel groep] =&gt; Onbekend
      [situatie in duitsland] =&gt; Onbekend
      [URL] =&gt; private
      [contactpersonen] =&gt; private
      [simplicate_id] =&gt; private
  )
  </code></pre>
  
  <p>Just once, but i want every instance of the loop stored in the array.</p>
  
  <p>My expectation is:</p>
  
  <pre><code> 1(
          [DEXCODE] =&gt; 
          [LAND] =&gt; Country_The Netherlands
          [CP GENDER] =&gt; heer
          [OROrganisation] =&gt; private
          [STRAATNAAM] =&gt; private
          [ORCITY] =&gt; private
          [OREMAIL] =&gt; private
          [ORWEBSITE] =&gt; private
          [CP_PHONE] =&gt; private
          [first_name] =&gt; private
          [last name] =&gt; private
          [FUNCTIE] =&gt; private
          [CPMobile] =&gt; private
          [CPEmail1] =&gt; private
          [ORPostcode] =&gt; private
          [ORTelephone] =&gt; private
          [ACTIEFINDUITSLAND] =&gt; Ja
          [GESPREKSVELDSIMPLICATE] =&gt; private
          [organisatie grootte] =&gt; Onbekend
          [B2B of B2C] =&gt; Onbekend
          [doel groep] =&gt; Onbekend
          [situatie in duitsland] =&gt; Onbekend
          [URL] =&gt; private
          [contactpersonen] =&gt; private
          [simplicate_id] =&gt; private
      )
      2(
          [DEXCODE] =&gt; 
          [LAND] =&gt; Country_The Netherlands
          [CP GENDER] =&gt; heer
          [OROrganisation] =&gt; private
          [STRAATNAAM] =&gt; private
          [ORCITY] =&gt; private
          [OREMAIL] =&gt; private
          [ORWEBSITE] =&gt; private
          [CP_PHONE] =&gt; private
          [first_name] =&gt; private
          [last name] =&gt; private
          [FUNCTIE] =&gt; private
          [CPMobile] =&gt; private
          [CPEmail1] =&gt; private
          [ORPostcode] =&gt; private
          [ORTelephone] =&gt; private
          [ACTIEFINDUITSLAND] =&gt; Ja
          [GESPREKSVELDSIMPLICATE] =&gt; private
          [organisatie grootte] =&gt; Onbekend
          [B2B of B2C] =&gt; Onbekend
          [doel groep] =&gt; Onbekend
          [situatie in duitsland] =&gt; Onbekend
          [URL] =&gt; private
          [contactpersonen] =&gt; private
          [simplicate_id] =&gt; private
      )
      3(
          [DEXCODE] =&gt; 
          [LAND] =&gt; Country_The Netherlands
          [CP GENDER] =&gt; heer
          [OROrganisation] =&gt; private
          [STRAATNAAM] =&gt; private
          [ORCITY] =&gt; private
          [OREMAIL] =&gt; private
          [ORWEBSITE] =&gt; private
          [CP_PHONE] =&gt; private
          [first_name] =&gt; private
          [last name] =&gt; private
          [FUNCTIE] =&gt; private
          [CPMobile] =&gt; private
          [CPEmail1] =&gt; private
          [ORPostcode] =&gt; private
          [ORTelephone] =&gt; private
          [ACTIEFINDUITSLAND] =&gt; Ja
          [GESPREKSVELDSIMPLICATE] =&gt; private
          [organisatie grootte] =&gt; Onbekend
          [B2B of B2C] =&gt; Onbekend
          [doel groep] =&gt; Onbekend
          [situatie in duitsland] =&gt; Onbekend
          [URL] =&gt; private
          [contactpersonen] =&gt; private
          [simplicate_id] =&gt; private
      )
  </code></pre>