dongsuoying9059 2015-02-08 08:34
浏览 66
已采纳

删除php数组中的空值

my DB as follows,

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

and i'm using following sql query to get DB values.

$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");

and it goes though following php query,

$arr = array();
While($row1 = mysqli_fetch_assoc($sql)){
     $arr[] = $row1;
} 
$ar = array_values($arr);

and it gives out put like this,

[{"label":"income","value":"10000"},{"label":"bank charges","value":""},{"label":"rent","value":""},{"label":"bonus","value":"500"}]     

and I'm passing that values to java script(Morris.js) to show it in the donut chart. but it doesn't work. when there values as,

 [{"label":"income","value":"10000"},{"label":"bonus","value":"500"}] 

it works fine.i cant put "" values as "0" because there is no actual income as "bank charges" or "rent" (they are expenses) is there any method to remove this empty values ({"label":"bank charges","value":""} ) from above and get out put as,

 [{"label":"income","value":"10000"},{"label":"bonus","value":"500"}] 

and my morris.js code as follows,

<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>

thank you..

  • 写回答

2条回答 默认 最新

  • douce1368 2015-02-08 08:45
    关注

    Why not just use a loop to filter the empty items?

    $legalArray = array();
    foreach($ar as $item){
        if(is_numeric($item['value']))
            $legalArray[] = $item;
    }
    

    Output the $legalArray.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?