简体   繁体   English

如何合并数组? array_merge不起作用

[英]how to merge array? array_merge not working

hello everybody im trying to merge aa 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(); 因此,当我dd()时,我得到输出 like: 喜欢: 在此处输入图片说明

so what i want to do now is to merge all that array to become one in array:16> itself. 所以我现在要做的是将所有数组合并为array:16>本身。
how can i do that? 我怎样才能做到这一点? i have tried array merge and it isnt working either 我已经尝试了数组合并,但它既不工作

For your case you should have a unique $match->date_access so you can use it as a key of your array, like this : 对于您的情况,您应该具有唯一的$match->date_access以便可以将其用作数组的键,如下所示:

$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_collapse helper将数组的数组折叠为单个数组,下面是一个示例:

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

// [1, 2, 3, 4, 5, 6, 7, 8, 9]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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