簡體   English   中英

從數組中刪除空數組

[英]Remove empty arrays from array

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

我怎樣才能刪除上面的空數組?

請幫我從結果數組中刪除這些子數組

謝謝

您可以更改此行:

 $data[] = $rows;

對此:

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

這將檢查$rows是否不是空數組,然后將其推入$data數組

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM