当我通过[{"product":"TEST_1","best_selling_product":"305"},{"product":"IPHONE 4S","best_selling_product":"108"}]
返回数组时,但是Highcharts不允许我使用字符串作为值,那么如何将其更改为int或float?我的后端功能是这个
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();
}
答案 0 :(得分:3)
在json_encode()函数中,设置JSON_NUMERIC_CHECK标志。
答案 1 :(得分:2)
您可以在mysql查询中使用CAST
,也可以使用floatval()
或intval()
在php中处理数据。
例如在mysql查询中:
SELECT CAST(SUM(sale_detail.amount) as UNSIGNED) AS best_selling_product FROM ...
或与php:
while($record = $result->fetch_array(MYSQLI_ASSOC)){
$record['best_selling_product'] = floatval($record['best_selling_product']);
$array[] = $record;
}