First array element getting overwritten by the second when getting data from db using PHP
<p>I have a few lines of code that start something like this:</p>

<pre><code>$trailheads = array();
// Then a db call with a query.  Then loop through the results.

// This gives a diff value every time, so here we are still ok
$trailhead-&gt;trailhead_name = $row['trailhead_name']; 

// Before the look iteration ends, I do something like this:
array_push ( $trailheads , $trailhead );
// But I could have done this with the same result:
$trailheads[] = $trailhead; 
</code></pre>

<p>And once I exit the loop, I do print_r and it shows that the second of the two rows returned by the query over-wrote the first.</p>

<p>Here is the full version of the loop:</p>

<pre><code>    while($row = mysql_fetch_assoc($trailhead_result))
    {
        $trailhead-&gt;trailhead_name = $row['trailhead_name'];


        $trailhead-&gt;park_id = $row['park_id'];
        $trailhead-&gt;trailhead_id = $row['trailhead_id'];
        $trailhead-&gt;trailhead_description = $row['trailhead_description'];
        $trailhead-&gt;parking = $row['parking'];
        $trailhead-&gt;lat = $row['lat'];
        $trailhead-&gt;lng = $row['lng'];
        $trailhead-&gt;is_free = $row['is_free'];
        $trailhead-&gt;parking_spots = $row['parking_spots'];
        $trailhead-&gt;cost_details = $row['cost_details'];

        $trailheads[] = $trailhead;
    }
</code></pre>
