title: PHP Array: Contents from result set
body: <p>I have a fairly simple problem to which I've been unable to find a solution.  I'd like to fill an array with the contents of a mySQL column.</p>
  
  <p>Basically I have a loop that gathers the result set, and I need to take a specific column from this result set and put it into an array.</p>
  
  <pre><code>    foreach ($results as $row){
  
        $names = array();
  
        $names[] = $row['name'];
  
      };
  </code></pre>
  
  <p>Let's say there were 40 names in the result set they should now all be in the $names array, but I only get the last result when I attempt to print the contents on screen:  </p>
  
  <pre><code>    echo $names[0]; or print_r($names);
  </code></pre>
  
  <p>I have also tried:</p>
  
  <pre><code>    while($row = mysql_fetch_array($results)) {
  
         $names[] = $row['name']; 
  
      }
  </code></pre>
  
  <p>However I already have the foreach setup and working so I don't want to introduce another loop just to populate the array.  </p>
  
  <p>I know the query and loop are both working because I can echo out the value of each name directly by placing: </p>
  
  <pre><code>    echo $row['name'] 
  </code></pre>
  
  <p>in the loop and see the resulting names print on screen.</p>