简体   繁体   English

从数组中删除空数组

[英]Remove empty arrays from array

I have ajaxlistAction:我有 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));
}

response of this action is like this这个动作的反应是这样的

{"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"]]}

And I want to remove empty arrays and want output like this我想删除空数组并想要这样的输出

{"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"]]}

How could I remove the empty arrays in above?我怎样才能删除上面的空数组?

Please help me to remove these sub-arrays from the resulting array请帮我从结果数组中删除这些子数组

Thanks谢谢

You can change this line:您可以更改此行:

 $data[] = $rows;

To this:对此:

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

That will check if $rows is not an empty array then push it into $data array这将检查$rows是否不是空数组,然后将其推入$data数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM