2016-05-16 58 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; 
}