首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony 3-2数组间的差异

Symfony 3-2数组间的差异
EN

Stack Overflow用户
提问于 2017-02-02 15:45:01
回答 2查看 7.2K关注 0票数 3

有办法区分2 ArrayCollection吗?(如array_diff)

今天,我循环第一个,并检查$ it -> if () match,但是我认为它可以重构。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-04 08:16:17

您可以通过以下方式使用array_diff:

代码语言:javascript
复制
$diff = array_diff($arrayCollection1->toArray(), $arrayCollection2->toArray());

$arrayCollectionDiff = new ArrayCollection($diff);
票数 8
EN

Stack Overflow用户

发布于 2018-09-23 11:34:44

我建议如下:

由两个函数组成的类:

  • 函数getElementFromANotInB给出了ArrayCollection A中的元素,而不是ArrayCollection B中的元素(也可以反过来用于从B中获取元素,而不是在A中);
  • 函数getElementCommonInAAndB给出了ArrayCollection A和B的公共元素。

这是一堂课:

代码语言:javascript
复制
<?php
namespace [Bundle name]\DependencyInjection\ToolBox\ArrayCollectionTB;
use Doctrine\Common\Collections\ArrayCollection;
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;
        });

    }
}
?>

下面是它附带的测试类:

代码语言:javascript
复制
<?php
namespace Tests\[Bundle name]\DependencyInjection\ToolBox\ArrayCollectionTB;

use [Bundle name]\DependencyInjection\ToolBox\ArrayCollectionTB\ArrayCollectionTB;
use Doctrine\Common\Collections\ArrayCollection;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

/**
* @coversDefaultClass [Bundle name]\DependencyInjection\ToolBox\ArrayCollectionTB
*/
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);
    }
}
?>
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42006450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档