当我返回我的数组时,我得到了[{"product":"TEST_1","best_selling_product":"305"},{"product":"IPHONE 4S","best_selling_product":"108"}]
,但是Highcharts不允许我使用字符串作为值,那么我如何将其更改为整型或浮点型呢?我的后端函数是
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();
}
发布于 2016-05-16 10:16:04
在json_encode()函数中,设置JSON_NUMERIC_CHECK标志。
发布于 2016-05-16 03:30:10
您可以在mysql查询中使用CAST
,也可以在php中使用floatval()
或intval()
处理数据。
例如,在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;
}
https://stackoverflow.com/questions/37245586
复制相似问题