有没有办法区分 2 ArrayCollection?(如array_diff)
今天我循环第一个并检查 $it->contains() 是否匹配,但我认为它可以重构。
您可以
通过以下方式使用array_diff:
$diff = array_diff($arrayCollection1->toArray(), $arrayCollection2->toArray());
$arrayCollectionDiff = new ArrayCollection($diff);
我会提出以下建议:
由两个函数组成的类:
- 函数
- (也可以反过来用于从 B 中获取不在 A 中的元素);
- 函数
getElementCommonInAAndB
给出了 ArrayCollection A 和 B 的常见元素。
getElementFromANotInB
给出 ArrayCollection A 中不在 ArrayCollection B 中的元素这是类:
<?php
namespace [Bundle name]DependencyInjectionToolBoxArrayCollectionTB;
use DoctrineCommonCollectionsArrayCollection;
class ArrayCollectionTB {
public function __construct(){}
public function getElementFromANotInB(ArrayCollection $acA,ArrayCollection $acB){
return $elementsInANotInB = $acA->filter(function($a) use ($acB) {
return $acB->contains($a)===false;
});
}
public function getElementCommonInAAndB(ArrayCollection $acA,ArrayCollection $acB){
return $elementsCommonInAAndB = $acA->filter(function($a) use ($acB) {
return $acB->contains($a)===true;
});
}
}
?>
这是它附带的测试类:
<?php
namespace Tests[Bundle name]DependencyInjectionToolBoxArrayCollectionTB;
use [Bundle name]DependencyInjectionToolBoxArrayCollectionTBArrayCollectionTB;
use DoctrineCommonCollectionsArrayCollection;
use PHPUnitFrameworkTestCase;
use SymfonyBundleFrameworkBundleTestKernelTestCase;
/**
* @coversDefaultClass [Bundle name]DependencyInjectionToolBoxArrayCollectionTB
*/
class ArrayCollectionTBTest extends KernelTestCase{
private static $acA;
private static $acB;
private static $acTB;
public static function setUpBeforeClass()
{
//start the symfony kernel
$kernel = static::createKernel();
$kernel->boot();
//get the DI container
$container = $kernel->getContainer();
self::$acA = new ArrayCollection();
self::$acB = new ArrayCollection();
//acA and acB have in common 1,5
//acA has 2 and 3 not in acB
self::$acA->add('element 1');
self::$acA->add('element 2');
self::$acA->add('element 3');
self::$acA->add('element 5');
//acB has 4 and 6 not in acA
self::$acB->add('element 1');
self::$acB->add('element 4');
self::$acB->add('element 5');
self::$acB->add('element 6');
self::$acTB = new ArrayCollectionTB();
}
/**
* @covers ::getElementFromANotInB
* @testdox test check dif give element from ArrayCollection A not in ArrayCollection B
*/
public function testGetElementFromANotInB(){
$res = self::$acTB->getElementFromANotInB(self::$acA,self::$acB);
//result should be 2 and 3
$this->assertNotContains('element 1',$res);
$this->assertContains('element 2',$res);
$this->assertContains('element 3',$res);
$this->assertNotContains('element 4',$res);
$this->assertNotContains('element 5',$res);
$this->assertNotContains('element 6',$res);
}
/**
* @covers ::getElementFromANotInB
* @testdox test check dif give element from ArrayCollection B not in ArrayCollection A
*/
public function testGetElementFromBNotInA(){
$res = self::$acTB->getElementFromANotInB(self::$acB,self::$acA);
$this->assertNotContains('element 1',$res);
$this->assertNotContains('element 2',$res);
$this->assertNotContains('element 3',$res);
$this->assertContains('element 4',$res);
$this->assertNotContains('element 5',$res);
$this->assertContains('element 6',$res);
}
/**
* @covers ::getElementCommonInAAndB
* @testdox test check gives element from ArrayCollection A and ArrayCollection A
*/
public function testGetElementFromBAndA(){
$res = self::$acTB->getElementCommonInAAndB(self::$acB,self::$acA);
$this->assertContains('element 1',$res);
$this->assertNotContains('element 2',$res);
$this->assertNotContains('element 3',$res);
$this->assertNotContains('element 4',$res);
$this->assertContains('element 5',$res);
$this->assertNotContains('element 6',$res);
}
}
?>
当 ArrayCollection 是一个对象数组时:
$collFoo = ArrayCollection($arrayOfFooObjects);
$collBar = ArrayCollection($arrayOfBarObjects);
$diff = $collFoo->filter(function (Foo $fooObject) use ($collBar) {
return !$collBar->contains($fooObject);
});
简单array_diff
引发错误:Error: Object of class Foo could not be converted to string