I can’t properly overwrite the array

I fill the first array like this:

foreach ($_POST['case'] as $key => $value) { $tmp = DB::table('case_garage')->where('id_case', $key)->get(); $garage [] = ['id_garage' => $tmp[0]->id_garage, ['id_case' => $key]]; } 

something like this turns out:

 array(20) { [0]=> array(2) { ["id_garage"]=> int(2) [0]=> array(1) { ["id_case"]=> int(48) } } [1]=> array(2) { ["id_garage"]=> int(2) [0]=> array(1) { ["id_case"]=> int(49) } } [2]=> array(2) { ["id_garage"]=> int(2) [0]=> array(1) { ["id_case"]=> int(50) } } [3]=> array(2) { ["id_garage"]=> int(3) [0]=> array(1) { ["id_case"]=> int(51) } } 

I need an array of this type:

 id_garage => 2 ( id_case => 48, id_case => 49... id_garage => 4 ( id_case => 51) 

Fill in this way:

 foreach ($garage as $key => $value) { $buf [$value['id_garage']][] = ['id_case' => $value[0]['id_case']]; } 

It turns out a little bit wrong. How to form such a multidimensional array?

 array(2) { [2]=> array(17) { [0]=> array(1) { ["id_case"]=> int(48) } [1]=> array(1) { ["id_case"]=> int(49) } [3]=> array(3) { [0]=> array(1) { ["id_case"]=> int(51) } 
  • id_garage => 2 ( id_case => 48, id_case => 49... , here the same id_case keys cannot be like this ... - Manitikyl
  • probably better then do this: [2] => ('id_case' => [48, 49]) - Manitikyl
  • @Manitikyl how then to implement such an architecture? What would all id_case that belong to id_ garage be attached to? - sbaikov pm
  • Try this: $buf [$value['id_garage']]['id_case'][] = $value[0]['id_case']; . If there is no more data in the array, then it is possible and there is no point in ['id_case'] and you can delete it because it’s clear what the values ​​are. - Manitikyl 2:17 pm

0