Sherin Sherin - 2 years ago 148
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();
$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 Source

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.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download