简体   繁体   中英

Codeigniter return mysql query result to single array

I have code like this

$mene   = date('Y-m-d h:i:s', strtotime('+1 days'));
$now    = date('Y-m-d h:i:s');
$qnow   = $this->db->query("SELECT pilihan,COUNT(pilihan) as total FROM votes WHERE date_create BETWEEN '$now' AND '$mene' GROUP BY pilihan");

$someArray = [];
foreach($qnow->result_array() as $row){
    array_push($someArray, [
    $row['pilihan']   => $row['total']
    ]);
}

$someJSON = json_encode($someArray);
echo $someJSON;

And this for result

[
  {
    "1": "213"
  },
  {
    "2": "444"
  }
]

How to make this result to single array, so result will be

[
  {
    "1": "213",
    "2": "444"
  }
]

Please help to resolve that, Thank you.

Inside foreach loop try replacing the below code:

array_push($someArray, [$row['pilihan']   => $row['total']]);

to:

$someArray[$row['pilihan']] = $row['total'];

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