-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 に答える