douyueqing1530 2017-11-10 17:05
浏览 55
已采纳

Codeigniter将mysql查询结果返回给单个数组

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.

  • 写回答

1条回答 默认 最新

  • dprnr5559 2017-11-10 17:11
    关注

    Inside foreach loop try replacing the below code:

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

    to:

    $someArray[$row['pilihan']] = $row['total'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用vmmare虚拟机用sentaurus仿真的时候,调用terminal程序,输入swb指令弹出这个,打不开workbench,桌面上面的sentaurus workbench也打不开
  • ¥75 使用winspool.drv的SetPrinter设置打印机失败
  • ¥15 simulink 硬件在环路仿真
  • ¥15 python动态规划:N根火柴摆出的最大数字
  • ¥20 (标签-excel)
  • ¥200 求idea10和MyEclipse7.1
  • ¥20 vb6.0截取当前窗体保存为jpg文件
  • ¥20 苹果手机不使用大疆sdk怎么获取遥控器控制信息或如何接入大疆sdk并且成功上架sdk
  • ¥20 woocommerce 注册按键重定向
  • ¥100 求书法图像文字切割代码