doumu6997 2018-07-20 11:54
浏览 97

如何在php中将数组推送到多维数组

I have a array with bill details. I fetched the reciept(doc_no) of that perticular billno in another array.

I want an output as a multi dimensional array, that is in the array it will show bill data with the matching reciept data to that bill.

Expected output :

bill101: {
    ballance : 100,
    paid : 100,
    date:2018,
    doc_no101:{ 
        // reciept data of matching bill number will come here
    }
    doc_no102:{ 
        // reciept data of matching bill number will come here
    }        
} 

bill102: {        
    ballance : 100,
    paid : 100,
    date:2018,
    doc_no103:{ 
        // reciept data of matching bill number will come here
    }
    doc_no105:{ 
        // reciept data of matching bill number will come here
    }        
} 

How I wrote my code

First I will fetch bill table data and I will store in a array

$bill_data=DB::table('bills')
    ->distinct()
    ->select('bill_no','paid','total_amount','ballance','date')
    ->get();

Next I will do foreach loop and store it in a separate array. I will repeat these 2 steps for second query that is reciept data.

Finally I will push these array to a single array. But I am not getting the perfect output

My code

$bill_data=DB::table('bills')
    ->distinct()
    ->select('bill_no','paid','total_amount','ballance','date')
    ->get();

$finalOutput = [];
$temp = [];

foreach ($bill_data as $b) {
    $dumy = array(
        $b->bill_no=>[ 
           'paid' => $b->paid, 
           'total_amount' => $b->total_amount, 
           'ballance' => $b->ballance, 
           'date' => '222']
    );
    array_push($finalOutput, $dumy);
    $reciept_data=DB::table('reciepts')
        ->where('bill_no', $b->bill_no)
        ->get();

    foreach ($reciept_data as $r) {
        $reciept=array(
            $r->doc_no=>[ 
                'paid' => $r->paid, 
                'ballance' => $r->ballance]
            );

            array_push($temp,$reciept);
    }
    array_push($finalOutput, $temp);
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制
    • ¥20 usb设备兼容性问题
    • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
    • ¥15 安装svn网络有问题怎么办