Home >Database >Mysql Tutorial >How to Construct PHP Arrays from MySQL Column Data?
Constructing PHP Arrays from MySQL Column Data
Retrieving data from a MySQL column using mysql_fetch_array results in an array representing a single row. To create an array consisting of values from all rows in a specific column, an effective approach involves iterating through the array and assembling a new array:
<code class="php">$column = array(); while ($row = mysql_fetch_array($info)) { $column[] = $row[$key]; // Append the value of the column being accessed to the new array }</code>
By incrementally adding values from each row to the $column array, you can create an array that encompasses the entire column's data. Remember to specify the appropriate $key to indicate the column from which you want to extract values.
The above is the detailed content of How to Construct PHP Arrays from MySQL Column Data?. For more information, please follow other related articles on the PHP Chinese website!