从数组中删除空数组

时间:2015-10-19 16:00:28

标签: php arrays symfony null empty-list

我有ajaxlistAction:

public function ajaxlistAction()
{   
   $em = $this->getDoctrine()->getEntityManager();
   $qb = $em->createQueryBuilder();
   $qb->select('wfsd','b')
    ->from('TFTDataBundle:WpFormSubmittedData','wfsd')
    ->leftjoin('wfsd.customFieldData','b')
    ->leftjoin('b.custom_field','CustomField')
    ->getQuery()->getResult();


    if (!empty($_GET['sSearch'])) {
        $qb->andWhere('b.data like :search')->setParameter('search', "%{$_GET['sSearch']}%");
    }

    $qc = clone ($qb);
    $qc->select('COUNT( DISTINCT b.id)');
    $count = $qc->getQuery()->getSingleScalarResult();

    $columns = array('CustomField.fieldLabel');

    if( isset($_GET['iSortCol_0'] )){
        $dir = empty($_GET['sSortDir_0'])?'asc':$_GET['sSortDir_0'];
        $qb->orderBy($columns[$_GET['iSortCol_0']], $dir );
    }


    if (empty($_GET['iDisplayLength'])) {
        $_GET['iDisplayLength'] = 10;
    }
    if (empty($_GET['sEcho'])) {
        $_GET['sEcho'] = 1;
    }
    if (!empty($_GET['iDisplayStart'])) {
        $qb->setFirstResult($_GET['iDisplayStart']);
    }

    $qb->setMaxResults($_GET['iDisplayLength']);

    $data = array();

    foreach($qb->getQuery()->getResult() as $row ) {

        $rows = array();

        foreach ($row->getCustomFieldData() as $customFieldData) {
            $rows[] = $customFieldData->getData();

        }

        $data[] = $rows;
    }

    //$data = $qb->getQuery()->getArrayResult();
    return new JsonResponse(array("sEcho"=>$_GET['sEcho'],"iTotalRecords"=>$count,"iTotalDisplayRecords"=>$count,'aaData'=>$data));
}

此操作的响应就像这样

{"sEcho":"1","iTotalRecords":"44","iTotalDisplayRecords":"44","aaData":[[],[],[],[],[],[],[],["Name","email@domain.co","this is subject","this is message"],["sdsd","sas@ggkm.com","asa","sdsd"],["sdsd","sas@ggkm.com","asa","sdsd"]]}

我想删除空数组,并希望像这样输出

{"sEcho":"1","iTotalRecords":"44","iTotalDisplayRecords":"44","aaData":[["Name","email@domain.co","this is subject","this is message"],["sdsd","sas@ggkm.com","asa","sdsd"],["sdsd","sas@ggkm.com","asa","sdsd"]]}

我怎样才能删除上面的空数组?

请帮我从结果数组中删除这些子数组

由于

1 个答案:

答案 0 :(得分:0)

您可以更改此行:

 $data[] = $rows;

对此:

if( count($rows) > 0)
 $data[] = $rows;

这将检查$rows是否不是空数组,然后将其推入$data数组