Skip to content

IEnumerable.groupJoin() method

Marcel Kloubert edited this page Sep 30, 2015 · 11 revisions

IEnumerable->groupJoin($inner, $outerKeySelector, $innerKeySelector, $resultSelector [, $keyComparer]) method

Correlates the elements of that sequence and another based on matching keys and groups them (s. GroupJoin()).

Syntax

public function groupJoin(sequence $inner,
                          callable $outerKeySelector,
                          callable $innerKeySelector,
                          callable $resultSelector
                          [, equality_comparer $keyComparer = null ]) : IEnumerable;

Parameters

Name Type Description
$inner [[sequence Sequence]]
$outerKeySelector [[callable Callable]]
$innerKeySelector [[callable Callable]]
$resultSelector [[callable Callable]]
$keyComparer [[equality_comparer Equality comparer]]

$inner

The other sequence.

$outerKeySelector

The key selector for an item of that sequence that should be compared with the key of an item from the other sequence:

function (mixed $item, IIndexedItemContext $ctx) : mixed;

$item

The current outer item.

$ctx

The item context for $item.

$innerKeySelector

The key selector for an item of the other that should be compared with the key of an item from that sequence:

function (mixed $item, IIndexedItemContext $ctx) : mixed;

$item

The current inner item.

$ctx

The item context for $item.

$resultSelector

The selector that returns the result of corresponing items from the sequences.

function (mixed $outerItem, IEnumerable $innerItems) : mixed;

$outerItem

An item from that sequence.

$innerItems

The corresponding items of $outerItem from the other sequence.

$keyComparer

The equality comparer that checks if two item keys are equal or not.

Result

The new sequence.

Examples

use \System\Collections\IEnumerable;
use \System\Linq\Enumerable;

class Person {
    public function __construct($name) {
        $this->Name = $name;
    }
	
    public $Name;
}

class Pet {
    public function __construct($name, Person $owner) {
        $this->Name = $name;
        $this->Owner = $owner;
    }
	
    public $Name;
    public $Owner;
}

$persons = [new Person("Tanja"),
            new Person("Marcel"),
            new Person("Yvonne"),
            new Person("Josefine")];

$pets = [new Pet("Gina"     , $persons[1]),
         new Pet("Schnuffi" , $persons[1]),
         new Pet("Schnuffel", $persons[2]),
         new Pet("WauWau"   , $persons[0]),
         new Pet("Lulu"     , $persons[3]),
         new Pet("Sparky"   , $persons[0]),
         new Pet("Asta"     , $persons[1])];

$personSeq = Enumerable::create($persons);

$joined = $personSeq->groupJoin($pets,
                                '$person => $person->Name',
                                '$pet => $pet->Owner->Name',
                                function(Person $person, IEnumerable $pets) {
                                    $petList = $pets->select('$x => $x->Name')
                                                    ->joinToString('", "');

                                    return sprintf('Owner: %s; Pets: "%s"',
                                                   $person->Name,
                                                   $petList);
                                });

foreach ($joined as $item) {
    // [0] 'Owner: Tanja; Pets: "WauWau", "Sparky"'
    // [1] 'Owner: Marcel; Pets: "Gina", "Schnuffi", "Asta"'
    // [2] 'Owner: Yvonne; Pets: "Schnuffel"'
    // [3] 'Owner: Josefine; Pets: "Lulu"'
}
Clone this wiki locally