douyi0219
2019-07-10 03:20
浏览 77
已采纳

如何通过foreach获取数据并推送到数组?

I have some code, and I run it but it always includes a null array. How can I get only not null data?

public function index(Request $request)
{
    $list_no_invoice = Invoice::whereNotNull('invoice_no')->get();
    $data = array();
    foreach ($list_no_invoice as $key => $value) {
        $check = Data::where('invoice_no', $value->invoice_no)->get();
        array_push($data, $check);
    }

    return $data;
}

Returned Data

[[{"id":2,"bulan":"januari","tahun":"2018","nama_pkp":"Achmad Rivai","keterangan":"DP 50% Sewa periode 15 Feb - 13 Mar'16 ( Pameran Nands )","no_faktur_pajak":"010.000-16.43061871","tgl_faktur_pajak":"2016-03-18","ref":"BPnB No.154\\/III\\/2016","dpp":10227273,"ppn":1022727,"invoice_no":"IR16020115","nama_tenant":"Nand;s","pph_4_deyon":1022727,"pph_4_tenant":null,"bukpot_oleh_tenant":null,"no_p_l_tenant":null,"tgl_p_l_penyewa":"1970-01-01","pph_p_l_tenant":null,"note":null,"checklist":1022727,"selisih":"0.30","created_at":null,"updated_at":null,"deleted_at":null,"pph_23":null,"has_edit":0}],[{"id":3,"bulan":"januari","tahun":"2018","nama_pkp":"Achmad Rivai","keterangan":"Pelunasana Sewa periode 15 Feb - 13 Mar'16 ( Pameran Nands )","no_faktur_pajak":"010.000-16.43061872","tgl_faktur_pajak":"2016-03-18","ref":"BPnB No.154\\/III\\/2016","dpp":10227273,"ppn":1022727,"invoice_no":"IR16020116","nama_tenant":"Nand;s","pph_4_deyon":1022727,"pph_4_tenant":null,"bukpot_oleh_tenant":null,"no_p_l_tenant":null,"tgl_p_l_penyewa":"1970-01-01","pph_p_l_tenant":null,"note":null,"checklist":1022727,"selisih":"0.30","created_at":null,"updated_at":null,"deleted_at":null,"pph_23":null,"has_edit":0}],[{"id":4,"bulan":"januari","tahun":"2018","nama_pkp":"Achmad Rivai","keterangan":"DP 25% Sewa periode 22 Feb - 27 Mar'16 (Pameran Nands Purple)","no_faktur_pajak":"010.031-16.63924514","tgl_faktur_pajak":"2016-04-15","ref":"BPnB No.169\\/IV\\/2016","dpp":5017727,"ppn":501773,"invoice_no":"IR16020047","nama_tenant":"Nand's Purple","pph_4_deyon":501773,"pph_4_tenant":null,"bukpot_oleh_tenant":null,"no_p_l_tenant":null,"tgl_p_l_penyewa":"1970-01-01","pph_p_l_tenant":null,"note":null,"checklist":501773,"selisih":"-0.30","created_at":null,"updated_at":null,"deleted_at":null,"pph_23":null,"has_edit":0}],[],[],[],[],[],[],[]]

How to not include the data in the end [],[],[]...?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dongyou6795 2019-07-10 05:22
    已采纳
    foreach ($list_no_invoice as $key => $value) {
       $check = Data::where('invoice_no', $value->invoice_no)->get();
       if (sizeof($check) > 0) {
          array_push($data, $check);
       }
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanche8554 2019-07-10 03:26

    You can add check !empty() to dont add empty array data

    foreach ($list_no_invoice as $key => $value) {
       $check = Data::where('invoice_no', $value->invoice_no)->get();
       if (!empty($check)) {
          array_push($data, $check);
       }
    }
    
    评论
    解决 无用
    打赏 举报
  • doufeng5059 2019-07-10 04:14

    before push add check count($check) > 0

    评论
    解决 无用
    打赏 举报
  • duandan1995 2019-07-10 13:25

    Actualy you can do this very easly without any loop:

    $list_no_invoice = Invoice::whereNotNull('invoice_no')->pluck('invoice_no')->toArray();
    //get all records where invoice_no is one of values from array $list_no_invoice
    $data = Data::whereIn('invoice_no',$list_no_invoice)->get()->toArray();
    return $data;
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题