-1

hello everybody im trying to merge a a few array in an array. currently with my code:

$matchs = DiraChatLog::where('status','=','Match')->whereBetween('date_access', [$request->from, $request->to])->get();
foreach ($matchs as $key => $match) {
    $array[] = [
        $match->date_access => $match->status,
    ];

}

dd($array);

so with this i get the ouput when i dd(); like: enter image description here

so what i want to do now is to merge all that array to become one in array:16> itself.
how can i do that? i have tried array merge and it isnt working either

Rajveer Singh
  • 433
  • 5
  • 20

1 Answers1

2

For your case you should have a unique $match->date_access so you can use it as a key of your array, like this :

$matchs = DiraChatLog::where('status','=','Match')
    ->whereBetween('date_access', [$request->from, $request->to])
    ->get();

foreach ($matchs as $key => $match) {
    $array[$match->date_access] = $match->status;
}

if you have a more complex data you can use array_collapse helper to collapses an array of arrays into a single array, here is an example :

$array = array_collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);

// [1, 2, 3, 4, 5, 6, 7, 8, 9]
Abdou Tahiri
  • 4,338
  • 5
  • 25
  • 38