Sherin Sherin - 7 months ago 37
PHP Question

PHP -convert one column in 2D array into 1D array specfying column name

I have a 2D array , which holds the result of a mysql query. here is my code

$res=$ $this->dbconnection->Query($Query);
$query_result= array();
while($colRow=mysqli_fetch_array($res))
{
$query_result[]= $colRow;
}


Now i want 1D array which contains all rows under a particular column in $query_result.
For example, the database table contains the fields Name and ID,I know ,
$query_result[]= $colRow['Name']
will give query results into ID. But I need all rows under Name and Id separately , such as
$name= $query_result['Name']
,
$Id= $query_result['ID']
.
Is there any easy way to accomplish this?

Answer

Since PHP 5.5.0 you can use...

$myfield_arr = array_column($query_result, 'myfield_name');

... to isolate a column from a two dimensional array.

See http://php.net/manual/en/function.array-column.php