title: How do I fill an array inside a while loop and get new scope each iteration?
body: <p>The problem is that I get only the last value comming from the Table. I think its because I am building the array while referencing its values to the same object, and it keeps changing. I know while loop doesnt create a new scope for each iteration which <strong>IS</strong> the problem. </p>
  
  <p><strong>What's the best way to get a new scope for each iteration?</strong></p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>    $namesArray= array();
          while ($row=mysql_fetch_array($result))
          {
          $nameAndCode-&gt;code = $row['country_code2'];
          $nameAndCode-&gt;name = $row['country_name'];           
          array_push($namesArray,$nameAndCode);
  
  
          } 
  return $namesArray;
  </code></pre>