如何合并两个 php Doctrine 2 ArrayCollection() [英] How to merge two php Doctrine 2 ArrayCollection()

查看:20
本文介绍了如何合并两个 php Doctrine 2 ArrayCollection()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方便的方法可以让我连接两个 Doctrine ArrayCollection()?类似:

Is there any convenience method that allows me to concatenate two Doctrine ArrayCollection()? something like:

$collection1 = new ArrayCollection();
$collection2 = new ArrayCollection();

$collection1->add($obj1);
$collection1->add($obj2);
$collection1->add($obj3);

$collection2->add($obj4);
$collection2->add($obj5);
$collection2->add($obj6);

$collection1->concat($collection2);

// $collection1 now contains {$obj1, $obj2, $obj3, $obj4, $obj5, $obj6 }

我只想知道我是否可以省去迭代第二个集合并将每个元素一个一个地添加到第一个集合中的过程.

I just want to know if I can save me iterating over the 2nd collection and adding each element one by one to the 1st collection.

谢谢!

推荐答案

对我来说更好(和工作)的变体:

Better (and working) variant for me:

$collection3 = new ArrayCollection(
    array_merge($collection1->toArray(), $collection2->toArray())
);

这篇关于如何合并两个 php Doctrine 2 ArrayCollection()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆