2016-05-16 26 views
1

當我返回我的數組我得到[{"product":"TEST_1","best_selling_product":"305"},{"product":"IPHONE 4S","best_selling_product":"108"}],但Highcharts不讓我使用字符串作爲價值,那麼我怎麼能改變它爲int或浮動?我的後端功能是本我該如何修改我的JSON結果?

function best_selling_product(){ 
     $sql = "SELECT product,SUM(sale_detail.amount) AS best_selling_product FROM sale_detail INNER JOIN product ON sale_detail.idproduct = product.idproduct GROUP BY sale_detail.idproduct ORDER BY SUM(sale_detail.amount) DESC LIMIT 0,5"; 
     $result = $this->conexion->conexion->query($sql); 
     $array = array(); 
     while($record = $result->fetch_array(MYSQLI_ASSOC)){ 
      $array[] = $record; 
     } 
     return $array; 
     $this->conexion->cerrar(); 
    } 

回答

2

您可以在MySQL查詢中使用CAST,或使用floatval()intval()處理PHP中的數據。

例如在MySQL查詢:

SELECT CAST(SUM(sale_detail.amount) as UNSIGNED) AS best_selling_product FROM ... 

OR用PHP:

while($record = $result->fetch_array(MYSQLI_ASSOC)){ 
    $record['best_selling_product'] = floatval($record['best_selling_product']); 
    $array[] = $record; 
}