Search code examples
phpsymfonydiffarraycollection

Symfony 3 - difference between 2 arraycollection


Is there a way to differentiate 2 ArrayCollection ? (like array_diff)

today I loop on the first and check if $it->contains() match, but I think it can be refactored.


Solution

  • You can use array_diff in the following way:

    $diff = array_diff($arrayCollection1->toArray(), $arrayCollection2->toArray());
    
    $arrayCollectionDiff = new ArrayCollection($diff);