I have this PDO code
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;
}
and show the data in my index php file like this one
$getAllUserTicketHistoryJson = getAllUserTicketHistoryJson($getRound['id']);
And call all the data to this one
foreach($getAllUserTicketHistoryJson as $key => $value){
$array=$value;
}
when I try to var_export($value); the data show like this
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', )
My question is, can I get the data display in one array?
should be like this
array("1" => 1, '2' => 50, '3' => 10, '4' => 5)
how to get the data become inside one array only?
Found the answer, we need a custom format in foreach.
so the code will be like this
$data = array();
foreach($getAllUserTicketHistoryJson as $value){
$data[$value['user_id']] = number_format((float)($value['total_ticket'] / $getAllTicketRound * 100), 2, '.', '');
}