title: Dynamically adding an associative multidimensional array to an array containing another associative multidimensional array?
body: <p>I am trying to dynamically generate an multidimensional array where the <code>$value</code> of the <code>$key=&gt;$value</code> pairs is another multidimensional array. The keys are going to generate dynamically through a select menu (which itself is dynamically generated, so I wont know the names of keys). </p>
  
  <p>It's for a WordPress plugin so there are some WP functions in the following code:</p>
  
  <pre><code>$var1 = $_POST['var1'];
  $var2 = $_POST['var2'];
  if (get_option('option_name') != null) {
       $option = get_option('option_name');
       $option[$var2] = array('sync'=&gt;1,'setting1'=&gt;$var2,'setting2'=&gt;$var1);
       update_option('option_name', $option[$var2]);
  
  } else {
      $option = array();
      $option[$var2] = array('sync'=&gt;1,'setting1'=&gt;$var2,'setting2'=&gt;$var1);
      update_option('option_name', $option[$var2]);
  }
  </code></pre>
  
  <p>Basically, the "else" part of this, indicating that get_option('option_name') is empty or not set, is working great and after the update_option function is called I get:</p>
  
  <pre><code>Array
      (
          [firstvar2] =&gt; Array
              (
                  [sync] =&gt; 1
                  [setting1] =&gt; firstvar2
                  [setting2] =&gt; firstvar1
              )
  
      )
  </code></pre>
  
  <p>Which is exactly what I want. The problem I am having is adding an additional associative array when this first one is already present. The 'if' part of the conditional is giving me this:</p>
  
  <pre><code>Array
      (
          [sync] =&gt; 1
          [setting1] =&gt; firstvar2 //(from the first run)
          [setting2] =&gt; firstvar1 //(from the first run)
          [secondvar2] =&gt; Array //(from the second run)
              (
                  [sync] =&gt; 1
                  [setting1] =&gt; secondvar2 //(from the second run)
                  [setting2] =&gt; secondvar1 //(from the second run)
              )
  
      )
  </code></pre>
  
  <p>How can I get this instead?:</p>
  
  <pre><code>Array
      (
          [firstvar2] =&gt; Array //first run
              (
                  [sync] =&gt; 1 //first run
                  [setting1] =&gt; firstvar2 //first run
                  [setting2] =&gt; firstvar1 //first run
              )
  
          [secondvar2] =&gt; Array //second run
              (
                  [sync] =&gt; 1 //second run
                  [setting1] =&gt; secondvar2 //second run
                  [setting2] =&gt; secondvar1 //second run
              )
  
      )
  </code></pre>