title: Creating Associative array (hardcoded key) from foreach in PHP
body: <p>I have an array called <code>$arr</code> containing some information about users. Using <code>$arr</code> I want to create a new associative array with specific keys. That's what I got so far:</p>
  
  <pre><code>$groups = [];
  foreach($arr as $val) {
      $groups['first_key_name'] = $val-&gt;ID;
      $groups['second_key_name'] = $val-&gt;login;
  }
  </code></pre>
  
  <p>What I'm trying to achieve is a new array that has the following format:</p>
  
  <pre><code>'first_key_name' =&gt; $val-&gt;ID
  'second_key_name' =&gt; $val-&gt;login
  'first_key_name' =&gt; $val-&gt;ID
  'second_key_name' =&gt; $val-&gt;login
  </code></pre>
  
  <p>The problem with my current approach is when I <code>var_dump($groups)</code> I only get one key with an empty value although the array should contain at least 10 entries. </p>
  
  <p>The output of <code>var_dump($groups)</code>:</p>
  
  <pre><code>array:1 [▼
    "first_key_name" =&gt; "4"
  ]
  </code></pre>
  
  <p>What am I doing wrong?</p>