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

报告相同问题?