webCOMAND

collection::merge()

Merge an object, array/collection of objects or array/collection of arrays/collections of objects by appending all contained objects to the end of this collection, and return the new collection.

Prototype

collection merge(mixed $array = [], array $options = [])

Parameters

  • array - Optional object, or array/collection of objects to merge into the collection.
  • options - Associative array with zero or more of the following keys.
    • allow_duplicates - TRUE (default) if the array parameter contains the same object multiple times, include each instance in the collection.  FALSE will only include the first instance of each object.  This option only applies to this call.  Duplicate objects can still be added to the returned collection.

Return

Return the new merged collection.  The return value can be ignored in most cases because the original collection is updated as well.

Example

$a = $repo->get("FROM Contact WHERE Name LIKE 'A%'");
$b = $repo->get("FROM Contact WHERE Name LIKE '%Z'");

// merge collection $a and $b into a new collection $c
$c = $repo->new_collection([$a, $b], ['allow_duplicates'=>FALSE]);

// merge collection $b into collection $a
$a->merge($b, ['allow_duplicates'=>FALSE]);

Related

repo::new_collection()