How to merge two php Doctrine 2 ArrayCollection()

前端 未结 8 1031
误落风尘
误落风尘 2020-12-08 01:21

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

$collection1 = new ArrayCollection         


        
相关标签:
8条回答
  • 2020-12-08 02:04

    If you are required to prevent any duplicates, this snippet might help. It uses a variadic function parameter for usage with PHP5.6.

    /**
     * @param array... $arrayCollections
     * @return ArrayCollection
     */
    public function merge(...$arrayCollections)
    {
        $returnCollection = new ArrayCollection();
    
        /**
         * @var ArrayCollection $arrayCollection
         */
        foreach ($arrayCollections as $arrayCollection) {
            if ($returnCollection->count() === 0) {
                $returnCollection = $arrayCollection;
            } else {
                $arrayCollection->map(function ($element) use (&$returnCollection) {
                    if (!$returnCollection->contains($element)) {
                        $returnCollection->add($element);
                    }
                });
            }
        }
    
        return $returnCollection;
    }
    

    Might be handy in some cases.

    0 讨论(0)
  • 2020-12-08 02:07

    Better (and working) variant for me:

    $collection3 = new ArrayCollection(
        array_merge($collection1->toArray(), $collection2->toArray())
    );
    
    0 讨论(0)
  • 2020-12-08 02:07
    $newCollection = new ArrayCollection((array)$collection1->toArray() + $collection2->toArray()); 
    

    This should be faster than array_merge. Duplicate key names from $collection1 are kept when same key name is present in $collection2. No matter what the actual value is

    0 讨论(0)
  • 2020-12-08 02:08

    Using Clousures PHP5 > 5.3.0

    $a = ArrayCollection(array(1,2,3));
    $b = ArrayCollection(array(4,5,6));
    
    $b->forAll(function($key,$value) use ($a){ $a[]=$value;return true;});
    
    echo $a.toArray();
    
    array (size=6) 0 => int 1 1 => int 2 2 => int 3 3 => int 4 4 => int 5 5 => int 6
    
    0 讨论(0)
  • 2020-12-08 02:12

    You can simply do:

    $a = new ArrayCollection();
    $b = new ArrayCollection();
    ...
    $c = new ArrayCollection(array_merge((array) $a, (array) $b));
    
    0 讨论(0)
  • 2020-12-08 02:12

    You still need to iterate over the Collections to add the contents of one array to another. Since the ArrayCollection is a wrapper class, you could try merging the arrays of elements while maintaining the keys, the array keys in $collection2 override any existing keys in $collection1 using a helper function below:

    $combined = new ArrayCollection(array_merge_maintain_keys($collection1->toArray(), $collection2->toArray())); 
    
    /**
     *  Merge the arrays passed to the function and keep the keys intact.
     *  If two keys overlap then it is the last added key that takes precedence.
     * 
     * @return Array the merged array
     */
    function array_merge_maintain_keys() {
        $args = func_get_args();
        $result = array();
        foreach ( $args as &$array ) {
            foreach ( $array as $key => &$value ) {
                $result[$key] = $value;
            }
        }
        return $result;
    }
    
    0 讨论(0)
提交回复
热议问题