删除 php 数组中的空值
removing empty values in php arrays
我的数据库如下,
date income expenses maincat
2015-02-06 10000 salary
2015-02-05 500 bank charges
2015-02-05 300 rent
2015-02-01 500 bonus
我正在使用以下 sql 查询来获取数据库值。
$sql= mysqli_query($con,"SELECT maincat as label,income as value FROM transaction where date BETWEEN '$date1' AND '$date2' AND email='$user' GROUP BY maincat") or die ("error3");
它通过 php 查询,
$arr = array();
While($row1 = mysqli_fetch_assoc($sql)){
$arr[] = $row1;
}
$ar = array_values($arr);
它会像这样输出,
[{"label":"income","value":"10000"},{"label":"bank charges","value":""},{"label":"rent","value":""},{"label":"bonus","value":"500"}]
并且我将该值传递给 java 脚本 (Morris.js) 以在圆环图中显示它。但它不起作用。当有值时,
[{"label":"income","value":"10000"},{"label":"bonus","value":"500"}]
有效 fine.i 不能将“”值设为“0”,因为没有实际收入,因为 "bank charges" 或 "rent"(它们是支出)有没有任何方法可以删除这个空值 ({"label":"bank charges","value":""} ) 从上面出来,
[{"label":"income","value":"10000"},{"label":"bonus","value":"500"}]
和我的morris.js代码如下,
<script>
Morris.Donut({
element: 'donut',
resize : true,
data : <?php echo json_encode($ar); ?>,
backgroundColor: '#0011',
labelColor: 'black',
colors:[
'#A4A4A4','#FE2E64','#0B610B','#0B615E',
'#FF8000','#088A68','#4B8A08','#A9D0F5',
'#5F4C0B','#F3F781','#81BEF7','#04B431',
'#D7DF01','#BE85F7','#BE59F7','#BE81F7',
'#F4FA58','#0431B4','#D8D8D8','#4C0B5F',
'#086A87','#F7D358','#DF7401','#B18904',
'#045FB4',
],
});
</script>
谢谢..
为什么不直接使用循环来过滤空项?
$legalArray = array();
foreach($ar as $item){
if(is_numeric($item['value']))
$legalArray[] = $item;
}
输出 $legalArray。
你应该检查你的 sql 代码
SELECT maincat as label,income as value FROM transaction where date BETWEEN '$date1' AND '$date2' AND email='$user' AND label != '' AND value != '' GROUP BY maincat"
可能我的查询有误认为有更好的表达方式,
您也可以使用 php 进行检查
While($row1 = mysqli_fetch_assoc($sql)){
if (!empty($row1['label']) && !empty($row1['value']))
$arr[] = $row1;
}
我的数据库如下,
date income expenses maincat
2015-02-06 10000 salary
2015-02-05 500 bank charges
2015-02-05 300 rent
2015-02-01 500 bonus
我正在使用以下 sql 查询来获取数据库值。
$sql= mysqli_query($con,"SELECT maincat as label,income as value FROM transaction where date BETWEEN '$date1' AND '$date2' AND email='$user' GROUP BY maincat") or die ("error3");
它通过 php 查询,
$arr = array();
While($row1 = mysqli_fetch_assoc($sql)){
$arr[] = $row1;
}
$ar = array_values($arr);
它会像这样输出,
[{"label":"income","value":"10000"},{"label":"bank charges","value":""},{"label":"rent","value":""},{"label":"bonus","value":"500"}]
并且我将该值传递给 java 脚本 (Morris.js) 以在圆环图中显示它。但它不起作用。当有值时,
[{"label":"income","value":"10000"},{"label":"bonus","value":"500"}]
有效 fine.i 不能将“”值设为“0”,因为没有实际收入,因为 "bank charges" 或 "rent"(它们是支出)有没有任何方法可以删除这个空值 ({"label":"bank charges","value":""} ) 从上面出来,
[{"label":"income","value":"10000"},{"label":"bonus","value":"500"}]
和我的morris.js代码如下,
<script>
Morris.Donut({
element: 'donut',
resize : true,
data : <?php echo json_encode($ar); ?>,
backgroundColor: '#0011',
labelColor: 'black',
colors:[
'#A4A4A4','#FE2E64','#0B610B','#0B615E',
'#FF8000','#088A68','#4B8A08','#A9D0F5',
'#5F4C0B','#F3F781','#81BEF7','#04B431',
'#D7DF01','#BE85F7','#BE59F7','#BE81F7',
'#F4FA58','#0431B4','#D8D8D8','#4C0B5F',
'#086A87','#F7D358','#DF7401','#B18904',
'#045FB4',
],
});
</script>
谢谢..
为什么不直接使用循环来过滤空项?
$legalArray = array();
foreach($ar as $item){
if(is_numeric($item['value']))
$legalArray[] = $item;
}
输出 $legalArray。
你应该检查你的 sql 代码
SELECT maincat as label,income as value FROM transaction where date BETWEEN '$date1' AND '$date2' AND email='$user' AND label != '' AND value != '' GROUP BY maincat"
可能我的查询有误认为有更好的表达方式,
您也可以使用 php 进行检查
While($row1 = mysqli_fetch_assoc($sql)){
if (!empty($row1['label']) && !empty($row1['value']))
$arr[] = $row1;
}