title: Adding array object to an array in a foreach loop if a condition is satisfied in php
body: <p>I have an array like this.     </p>
  
  <pre><code>$array = [
         0 =&gt;[
             "name" =&gt; "John",
             "age" =&gt; "20",
             "class" =&gt; "first"
         ],
         1 =&gt; [
             "name" =&gt; "Doe",
              "age" =&gt; "22",
              "class" =&gt; "second"
         ],
         2 =&gt; [
              "name" =&gt; "Template",
              "age" =&gt; "30",
              "class" =&gt; "third"
          ],
      ];
  
  foreach ($array as $arr) {
      if ($arr["name"] == "Template") {
          $arr["result"] = ["maths" =&gt; "60", "English" =&gt; "20"];
          array_push($arr,$arr["result"]);
      }
  }
  </code></pre>
  
  <p>I have tried several things like using <code>array_push($arr,$arr["result"])</code> but that still returns the array.
  How can i loop through it to return something like this.</p>
  
  <pre><code>    2 =&gt; [
          "name" =&gt; "Template",
          "age" =&gt; "30",
          "class" =&gt; "third",
          "result"=&gt; ["maths" =&gt; "60", "English" =&gt; "20"],
      ],
  </code></pre>