简体   繁体   中英

Merge sql query same columns values into one array

I've this simple query.

SELECT purchase.Quantity*purchase.Price AS Total FROM purchase.

In code-igniter I fetch the result as array by: $this->db->query($query)->result_array();

The query result is:

array(2) { [0]=> array(1) { ["Total"]=> string(2) "42" } [1]=> array(1) { ["Total"]=> string(5) "10900" } }

What I want is a single array Total, which contains all the values like

Total=[42,10900]

Is there anything I'm missing in my code or query?

What you want is easily achievable by using a foreach loop. If I understood correctly..

$total = array();
foreach ($query->result_array() as $row) {
   $total[] = $row['Total'];
}
print_r($total); // outputs something like this: Array (  [0] => 42 [1] => 10900  )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM