简体   繁体   中英

how to create collection of array in php?

I need to create a colletion array .I use laravel in this case everything work , the route, the controller etc, the unique problem is that I need to the result have to similar to image

在此处输入图片说明

I try this code but dont works

$contacto_x_agendamientos = Contacto_x_Agendamiento::all();   
  $array = array( 
  foreach ($contacto_x_agendamientos as $contacto_x_agendamiento) {
     $array_2 = array(
         'id_contacto_agendamiento' =>$contacto_x_agendamiento->id_contacto_x_agendamiento
       );
   }
);
return response()->json($contacto_x_agendamientos->toArray());

I hope your help

You could use the [] shortcut

foreach ($contacto_x_agendamientos as $contacto_x_agendamiento) {
  $array_2[] = [
     'id_contacto_agendamiento' =>$contacto_x_agendamiento->id_contacto_x_agendamiento
  ];
}

in this way you popolate $array_2 with an array for each iteration

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM