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);
}