-1

我有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"]]}

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

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

谢谢

4

1 回答 1

0

您可以更改此行:

 $data[] = $rows;

对此:

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

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

于 2015-10-19T16:12:42.177 回答