我有这个PDO代码
function getAllUserTicketHistoryJson($rid){
global $conn;
$stmt = $conn->prepare("SELECT user_id, total_ticket FROM lottery_user WHERE round_id = :rou");
$stmt->bindParam(':rou', $rid);
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$res = $stmt->fetchAll();
return $res;
}
并在这样的索引php文件中显示数据
$getAllUserTicketHistoryJson = getAllUserTicketHistoryJson($getRound['id']);
然后将所有数据调用到这个
foreach($getAllUserTicketHistoryJson as $key => $value){
$array=$value;
}
当我尝试var_export($ value);数据显示如下
array ( 'user_id' => '1', 'total_ticket' => '1', )array ( 'user_id' => '2', 'total_ticket' => '50', )array ( 'user_id' => '3', 'total_ticket' => '10', )array ( 'user_id' => '4', 'total_ticket' => '5', )
我的问题是,我可以将数据显示在一个数组中吗?
应该是这样
array("1" => 1, '2' => 50, '3' => 10, '4' => 5)
如何使数据仅进入一个数组?
答案 0 :(得分:0)
找到答案了,我们需要在foreach中使用自定义格式。
所以代码将是这样
$data = array();
foreach($getAllUserTicketHistoryJson as $value){
$data[$value['user_id']] = number_format((float)($value['total_ticket'] / $getAllTicketRound * 100), 2, '.', '');
}