i want to generate a multi-dimentional array in controller for display data on view.
i tried this:-
$data2 = [];
foreach ($batch->details as $member_id){
$data2['details'][] = $member_id;
$invoices = $member_id->invoice_id;
$invoice_ids = explode(',', $invoices);
$payments = Payment::with('codes')->whereIn('id', $invoice_ids)-
>get();
$data2['details'][]['payments'] = $payments->toArray();
}
i get this type of array:-
'batch'=>array('id'=>1,
'name'=>"test",
"detail"=>array(0=>array( 'id'=>1,
'name'=>"tedsadst1",
'amount'=>30)
1=>array( 'id'=>3,
'name'=>"dsadsad",
'amount'=>30),
2=>array('id'=>4,
'name'=>"dsadasddsa",
'amount'=>30)
)
);
'payment'=>array(0=>array(0=>array('id'=>1,'name'=>"hello",
'amount'=>30,'other'=>"hello"
),
1=>array('id'=>3,'name'=>"demo",
'amount'=>330,'other'=>"world"
),
2 => array('id'=>7,'name'=>"dsad",
'amount'=>90,'other'=>"dasd")
)
)
i want this type of mutidimentional array:-
array('batch'=>array('id'=>1,
'name'=>"test",
"detail"=>array(0=>array('id'=>1,
'name'=>"test1",
'amount'=>30
"payment"=>array(0=>array(
0=>array('id'=>1,
'name'=>"hello",
'amount'=>30,
'other'=>"hello"),
1=>array('id'=>3,
'name'=>"demo",
'amount'=>330,
'other'=>"world"),
2 => array('id'=>7,
'name'=>"dsad",
'amount'=>90,
'other'=>"dasd")
)
)
),
1=>array('id'=>2,
'name'=>"test2",
'amount'=>60,
"payment"=>array(
0=>array(0=>array(
'id'=>9,
'name'=>"dsada",
'amount'=>852,
'other'=>"dsadsa"),
)
)
),
2=>array('id'=>3,
'name'=>"test3",
'amount'=>70.
"payment"=>array(
0=>array(0=>array(
'id'=>15,
'name'=>"hgfh",
'amount'=>30,
'other'=>"hehretllo"),
1=>array('id'=>17,
'name'=>"dsterad,
'amount'=>950,
'other'=>"gdfgg")
)
)
)
)
)
how to get multi-dimentional array like this using foreach or array functions i am using two foreach but i didn't get array look like this. so please help me. thank you
Try to make it like this.
$data2 = [];
foreach ($batch->details as $member_id){
$invoices = $member_id->invoice_id;
$invoice_ids = explode(',', $invoices);
$payments = Payment::with('codes')->whereIn('id', $invoice_ids)-
>get();
$newarray = array('details'=>$member_id,array('payments'=>$payments->toArray()));
$data2[] = $newarray;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.