Skip to content
Advertisement

codeigniter flatten array of query result

I’m using a query in Codeigniter to return the ids of all the rows that belong to a user.

JavaScript

This returns

JavaScript

Is it possible to return a flat array like

JavaScript

?

Advertisement

Answer

The CodeIgniter documentation (most particularly, query results) doesn’t list anything that will produce a flat array result (like the PDO::FETCH_COLUMN mode provided for PDOStatement::fetchAll). As such, array results will need to be flattened in your code, such as with a foreach loop:

JavaScript

or with the special purpose array_column:

JavaScript

or with the general purpose array_reduce:

JavaScript

If you’re to lazy to add few lines each time you select one column, you would need to tweak CodeIgniter. Like adding some option or returning flattened array when single column is selected. I would suggest adding an option

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement