【问题标题】:How to keep nested arrays on Laravel collection merge如何在 Laravel 集合合并中保留嵌套数组
【发布时间】:2020-05-26 19:37:39
【问题描述】:

我要疯了。

鉴于我有 2 个类似的数组


$array1 = [
    "data_to_merge" => ["2020-03-11 16:00:00", "2020-03-24 14:00:00"],
    "data_to_merge_past_year" => ["2019-03-11 16:00:00"],
];

$array2 = [
    "data_to_merge" => [],
    "data_to_merge_past_year" => ["2018-03-11 14:00:00"],
];

我的目标是得到类似的结果

all: [
       "data_to_merge" => [
         "2020-03-11 16:00:00",
         "2020-03-24 14:00:00"
       ],
       "data_to_merge_past_year" => [
         "2018-03-11 14:00:00",
         "2018-03-11 16:00:00",
       ],
     ],

我尝试了简单的

$result = collect($array1)->merge(collect($array2));

但是通过这种方法,它将从我的第一个数组中删除 data_to_merge 中的值。 Laravel 有什么方法可以得到这个结果吗?可以是2个以上的数组,只是为了演示。

这是我的完整示例,基本相同


$array1 = [
    "host" => "blablubb",
    "data_to_merge" => [
      "2020-03-11 16:00:00",
      "2020-03-24 14:00:00"
    ],
    "data_to_merge_past_year" => [
      "2019-03-11 16:00:00"
    ],
];

$array2 = [
    "host" => "blablubb",
    "data_to_merge" => [],
    "data_to_merge_past_year" => [
      "2018-03-11 16:00:00"
    ],
];

$array3 = [
    "host" => "blablubb",
    "data_to_merge" => [],
    "data_to_merge_past_year" => [],
];

$array4 = [
    "host" => "blablubb",
    "data_to_merge" => [
      "2020-03-04 14:00:00",
      "2020-03-04 17:00:00"
    ],
    "data_to_merge_past_year" => [],
];



$all = collect([$array1, $array2, $array3, $array4]);

$all
    ->groupBy('host')
    ->map(function ($item) {
        if (count($item) > 1) {
            $result = collect([]);

            foreach ($item as $subItem) {
                $result = $result->merge($subItem);
            }
            return $result;
        }

        return $item->first();
    })
    ->values()
    ->toArray();

谢谢大家!

【问题讨论】:

    标签: php laravel laravel-collection


    【解决方案1】:

    您可以为此使用mergeRecursive

    $array1 = [
        "data_to_merge" => ["2020-03-11 16:00:00", "2020-03-24 14:00:00"],
        "data_to_merge_past_year" => ["2019-03-11 16:00:00"],
    ];
    
    $array2 = [
        "data_to_merge" => [],
        "data_to_merge_past_year" => ["2018-03-11 14:00:00"],
    ];
    
    $result = collect($array1)->mergeRecursive(collect($array2));
    

    结果:

    Illuminate\Support\Collection {#1278 ▼
      #items: array:2 [▼
        "data_to_merge" => array:2 [▼
          0 => "2020-03-11 16:00:00"
          1 => "2020-03-24 14:00:00"
        ]
        "data_to_merge_past_year" => array:2 [▼
          0 => "2019-03-11 16:00:00"
          1 => "2018-03-11 14:00:00"
        ]
      ]
    }
    

    来自文档:

    mergeRecursive 方法合并给定的数组或集合 与原始集合递归。如果给定的字符串键 items 匹配原始集合中的字符串键,然后是值 因为这些键被合并到一个数组中,这就完成了 递归:

    $collection = collect(['product_id' => 1, 'price' => 100]);
    
    $merged = $collection->mergeRecursive(['product_id' => 2, 'price' => 200, 'discount' => false]);
    
    $merged->all();
    
    // ['product_id' => [1, 2], 'price' => [100, 200], 'discount' => false]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-28
      • 1970-01-01
      • 2013-07-01
      • 2018-10-10
      • 2017-03-07
      • 1970-01-01
      • 2020-02-21
      相关资源
      最近更新 更多