title: How to insert a new key-value pair in an associative array in PHP?
body: <p>I've an associative array named <code>$classes_data</code> as follows:</p>
  
  <pre><code>Array
  (
      [2] =&gt; Array
          (
              [class_id] =&gt; 2
              [class_name] =&gt; II
              [subjects] =&gt; Array
                  (
                      [0] =&gt; 11 Engllish
                  )
  
          )
  
      [3] =&gt; Array
          (
              [class_id] =&gt; 3
              [class_name] =&gt; III
              [subjects] =&gt; Array
                  (
                      [0] =&gt; Hidi
                      [1] =&gt; 11 Maths
                      [2] =&gt; 11 Science
                      [3] =&gt; 11 Engllish
                  )
  
          )
  
      [4] =&gt; Array
          (
              [class_id] =&gt; 4
              [class_name] =&gt; IV
              [subjects] =&gt; Array
                  (
                      [0] =&gt; Physics
                  )
  
          )
  
      [6] =&gt; Array
          (
              [class_id] =&gt; 6
              [class_name] =&gt; VI
              [subjects] =&gt; Array
                  (
                      [0] =&gt; Mathematics
                      [1] =&gt; dfadadadsagfasrsarasrarBiology
                  )
  
          )
  
      [7] =&gt; Array
          (
              [class_id] =&gt; 7
              [class_name] =&gt; VII
              [subjects] =&gt; Array
                  (
                      [0] =&gt; Physics
                      [1] =&gt; Chemistry11
                      [2] =&gt; 11 Science
                  )
  
          )
  
      [8] =&gt; Array
          (
              [class_id] =&gt; 8
              [class_name] =&gt; VIII
              [subjects] =&gt; Array
                  (
                      [0] =&gt; Hidi
                      [1] =&gt; 11 Engllish
                  )
  
          )
  
      [9] =&gt; Array
          (
              [class_id] =&gt; 9
              [class_name] =&gt; IX
              [subjects] =&gt; Array
                  (
                      [0] =&gt; Mathematics
                      [1] =&gt; Hidi
                      [2] =&gt; 11 Science
                  )
  
          )
  
  )
  </code></pre>
  
  <p>The keys of array (viz. 2,3,4,6,7,8,9) are in this manner instead of 0,1,2,3,4,5,6 because I've used one function to rearrange these keys.</p>
  
  <p>Now what I want to do is insert a new key <code>class_checked</code> and set its initial value as <code>0</code> (i.e.<code>class_checked =&gt;"0"</code>).</p>
  
  <p>I tried lot of tricks but couldn't get the desired array format. Can any one help me in this to get the desired array? Thanks in advance.</p>
  
  <p>For your information the required array format for array <code>$classes_data</code> will be as follows:</p>
  
  <pre><code> Array
      (
      [2] =&gt; Array
          (
              [class_id] =&gt; 2
              [class_name] =&gt; II
              [class_checked] =&gt; 0
              [subjects] =&gt; Array
                  (
                      [0] =&gt; 11 Engllish
                  )
  
          )
  
      [3] =&gt; Array
          (
              [class_id] =&gt; 3
              [class_name] =&gt; III
              [class_checked] =&gt; 0
              [subjects] =&gt; Array
                  (
                      [0] =&gt; Hidi
                      [1] =&gt; 11 Maths
                      [2] =&gt; 11 Science
                      [3] =&gt; 11 Engllish
                  )
  
          )
  
      [4] =&gt; Array
          (
              [class_id] =&gt; 4
              [class_name] =&gt; IV
              [class_checked] =&gt; 0
              [subjects] =&gt; Array
                  (
                      [0] =&gt; Physics
                  )
  
          )
  
      [6] =&gt; Array
          (
              [class_id] =&gt; 6
              [class_name] =&gt; VI
              [class_checked] =&gt; 0
              [subjects] =&gt; Array
                  (
                      [0] =&gt; Mathematics
                      [1] =&gt; dfadadadsagfasrsarasrarBiology
                  )
  
          )
  
      [7] =&gt; Array
          (
              [class_id] =&gt; 7
              [class_name] =&gt; VII
              [class_checked] =&gt; 0
              [subjects] =&gt; Array
                  (
                      [0] =&gt; Physics
                      [1] =&gt; Chemistry11
                      [2] =&gt; 11 Science
                  )
  
          )
  
      [8] =&gt; Array
          (
              [class_id] =&gt; 8
              [class_name] =&gt; VIII
              [class_checked] =&gt; 0
              [subjects] =&gt; Array
                  (
                      [0] =&gt; Hidi
                      [1] =&gt; 11 Engllish
                  )
  
          )
  
      [9] =&gt; Array
          (
              [class_id] =&gt; 9
              [class_name] =&gt; IX
              [class_checked] =&gt; 0
              [subjects] =&gt; Array
                  (
                      [0] =&gt; Mathematics
                      [1] =&gt; Hidi
                      [2] =&gt; 11 Science
                  )
  
          )
  
  )
  </code></pre>