In PHP, can I get a 1-dimensional array using PDO?

It seems that it should be (and probably) trivial. I have a simple query:

SELECT Name From User; 

When I run a query using this code:

 $rows = $preparedStatement->fetchAll(PDO::FETCH_ASSOC); 

$ The lines are as follows:

 Array ( [0] => Array ( [Name] => Doug ) [1] => Array ( [Name] => John ) ) 

Is there an easy way to make the array look something like this:

 Array( Doug, John) 
+6
source share
2 answers

Using the PDO :: FETCH_COLUMN constant:

 $columnNumber = 0; $rows = $preparedStatement->fetchAll(PDO::FETCH_COLUMN, $columnNumber); 

This way you get exactly what you offered.

You can also do the following:

 $columnNumber = 0; $rows = $preparedStatement->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE, $columnNumber); 

This way you get an array with unique values.

Source: http://www.php.net/manual/en/pdostatement.fetchall.php

+19
source

I think the correct answer was given by Jaison Erick, if you need to smooth out what you got (not recommended), it would do this:

 $flat = reset((call_user_func_array('array_merge_recursive', $rows))); 
+2
source

Source: https://habr.com/ru/post/900235/


All Articles