PHP Class YaLinqo\Enumerable

A sequence of values indexed by keys, which supports various operations: generation, projection, filtering, ordering, joining, grouping, aggregation etc.

To create a Enumerable, call {@link Enumerable::from} (aliased as a global function {@link from}) or any of the generation functions. To convert to array, call {@link Enumerable::toArrayDeep} or any of the conversion functions.

Internally, it is a wrapper around a lazily created iterator. The wrapped iterator is evaluated when {@link getIterator} is called.

See also: from
Inheritance: implements IteratorAggregate, use trait EnumerableGeneration, use trait EnumerablePagination
Show file Open project: athari/yalinqo Class Usage Examples

Public Methods

Method Description
aggregate ( callable $func, mixed $seed = null ) : mixed Applies an accumulator function over a sequence. If seed is not null, its value is used as the initial accumulator value.
aggregateOrDefault ( callable $func, mixed $seed = null, mixed $default = null ) : mixed Applies an accumulator function over a sequence. If seed is not null, its value is used as the initial accumulator value.
all ( callable $predicate ) : boolean Determines whether all elements of a sequence satisfy a condition.
any ( callable | null $predicate = null ) : boolean Determines whether a sequence contains any elements.
average ( callable | null $selector = null ) : number Computes the average of a sequence of numeric values.
call ( callable $action ) : Enumerable Invokes an action for each element in the sequence.
cast ( string $type ) : Enumerable Casts the elements of a sequence to the specified type.
contains ( $value ) : boolean Determines whether a sequence contains a specified element.
count ( callable | null $predicate = null ) : integer Returns the number of elements in a sequence.
distinct ( callable | null $keySelector = null ) : Enumerable Returns distinct elements from a sequence.
each ( callable $action = null ) Invokes an action for each element in the sequence.
except ( array | Iterator | IteratorAggregate | Enumerable $other, callable | null $keySelector = null ) : Enumerable Produces the set difference of two sequences. The set difference of two sets is defined as the members of the first set that do not appear in the second set.
getIterator ( ) : Iterator Retrieve an external iterator.
groupBy ( callable | null $keySelector = null, callable | null $valueSelector = null, callable | null $resultSelectorValue = null, callable | null $resultSelectorKey = null ) : Enumerable Groups the elements of a sequence by its keys or a specified key selector function.
groupJoin ( array | Iterator | IteratorAggregate | Enumerable $inner, callable | null $outerKeySelector = null, callable | null $innerKeySelector = null, callable | null $resultSelectorValue = null, callable | null $resultSelectorKey = null ) : Enumerable Correlates the elements of two sequences based on equality of keys and groups the results.
intersect ( array | Iterator | IteratorAggregate | Enumerable $other, callable | null $keySelector = null ) : Enumerable Produces the set intersection of two sequences. The intersection of two sets is defined as the set that contains all the elements of the first set that also appear in the second set, but no other elements.
join ( array | Iterator | IteratorAggregate | Enumerable $inner, callable | null $outerKeySelector = null, callable | null $innerKeySelector = null, callable | null $resultSelectorValue = null, callable | null $resultSelectorKey = null ) : Enumerable Correlates the elements of two sequences based on matching keys.
max ( callable | null $selector = null ) : number Returns the maximum value in a sequence of values.
maxBy ( callable $comparer, callable | null $selector = null ) : number Returns the maximum value in a sequence of values, using specified comparer.
min ( callable | null $selector = null ) : number Returns the minimum value in a sequence of values.
minBy ( callable $comparer, callable | null $selector = null ) : number Returns the minimum value in a sequence of values, using specified comparer.
ofType ( string $type ) : Enumerable Filters the elements of a sequence based on a specified type.
orderBy ( callable | null $keySelector = null, callable | integer | null $comparer = null ) : OrderedEnumerable Sorts the elements of a sequence in ascending order according to a key.
orderByDescending ( callable | null $keySelector = null, callable | integer | null $comparer = null ) : OrderedEnumerable Sorts the elements of a sequence in descending order according to a key.
orderByDir ( integer | boolean $sortOrder, callable | null $keySelector = null, callable | integer | null $comparer = null ) : OrderedEnumerable Sorts the elements of a sequence in a particular direction (ascending, descending) according to a key.
select ( callable $selectorValue, callable | null $selectorKey = null ) : Enumerable Projects each element of a sequence into a new form.
selectMany ( callable $collectionSelector = null, callable | null $resultSelectorValue = null, callable | null $resultSelectorKey = null ) : Enumerable Projects each element of a sequence to a sequence and flattens the resulting sequences into one sequence.
sum ( callable | null $selector = null ) : number Computes the sum of a sequence of values.
toArray ( ) : array Creates an array from a sequence.
toArrayDeep ( ) : array Creates an array from a sequence, traversing deeply.
toDictionary ( callable | null $keySelector = null, callable | null $valueSelector = null ) : array Creates an array from a sequence according to specified key selector and value selector functions.
toJSON ( integer $options ) : string Returns a string containing the JSON representation of sequence (converted to array).
toKeys ( ) : Enumerable Returns a sequence of keys from the source sequence.
toList ( ) : array Creates an array from a sequence, with sequental integer keys.
toListDeep ( ) : array Creates an array from a sequence, with sequental integer keys.
toLookup ( callable | null $keySelector = null, callable | null $valueSelector = null ) : array Creates an array from a sequence according to specified key selector and value selector functions.
toObject ( callable | null $propertySelector = null, callable | null $valueSelector = null ) : stdClass Transform the sequence to an object.
toString ( string $separator = '', callable | null $valueSelector = null ) : string Returns a string containing a string representation of all the sequence values, with the separator string between each element.
toValues ( ) : Enumerable Returns a sequence of values from the source sequence; keys are discarded.
union ( array | Iterator | IteratorAggregate | Enumerable $other, callable | null $keySelector = null ) : Enumerable Produces the set union of two sequences.
where ( callable $predicate ) : Enumerable Filters a sequence of values based on a predicate.
write ( string $separator = '', callable | null $selector = null ) Output the result of calling {@link toString} method.
writeLine ( callable | null $selector = null ) : string Output all the sequence values, with a new line after each element.

Protected Methods

Method Description
toArrayDeepProc ( $enum ) : array Proc for {@link toArrayDeep}.
toListDeepProc ( $enum ) : array Proc for {@link toListDeep}.
tryGetArrayCopy ( )

Private Methods

Method Description
__construct ( Closure | Iterator $iterator, boolean $isClosure = true )

Method Details

aggregate() public method

Syntax: aggregate (func {(a, v, k) ==> accum} [, seed])

Aggregate method makes it simple to perform a calculation over a sequence of values. This method works by calling func one time for each element in source. Each time func is called, aggregate passes both the element from the sequence and an aggregated value (as the first argument to func). If seed is null, the first element of source is used as the initial aggregate value. The result of func replaces the previous aggregated value. Aggregate returns the final result of func.

To simplify common aggregation operations, the standard query operators also include a general purpose count method, {@link count}, and four numeric aggregation methods, namely {@link min}, {@link max}, {@link sum}, and {@link average}.

public aggregate ( callable $func, mixed $seed = null ) : mixed
$func callable {(a, v, k) ==> accum} An accumulator function to be invoked on each element.
$seed mixed If seed is not null, the first element is used as seed. Default: null.
return mixed The final accumulator value.

aggregateOrDefault() public method

aggregateOrDefault (func {(a, v, k) ==> accum} [, seed [, default]])

Aggregate method makes it simple to perform a calculation over a sequence of values. This method works by calling func one time for each element in source. Each time func is called, aggregate passes both the element from the sequence and an aggregated value (as the first argument to func). If seed is null, the first element of source is used as the initial aggregate value. The result of func replaces the previous aggregated value. Aggregate returns the final result of func. If source sequence is empty, default is returned.

To simplify common aggregation operations, the standard query operators also include a general purpose count method, {@link count}, and four numeric aggregation methods, namely {@link min}, {@link max}, {@link sum}, and {@link average}.

public aggregateOrDefault ( callable $func, mixed $seed = null, mixed $default = null ) : mixed
$func callable {(a, v, k) ==> accum} An accumulator function to be invoked on each element.
$seed mixed If seed is not null, the first element is used as seed. Default: null.
$default mixed Value to return if sequence is empty. Default: null.
return mixed The final accumulator value, or default if sequence is empty.

all() public method

Syntax: all (predicate {(v, k) ==> result})

Determines whether all elements of a sequence satisfy a condition. The enumeration of source is stopped as soon as the result can be determined.

public all ( callable $predicate ) : boolean
$predicate callable {(v, k) ==> result} A function to test each element for a condition.
return boolean true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false.

any() public method

Syntax: any ()

Determines whether a sequence contains any elements. The enumeration of source is stopped as soon as the result can be determined.

Syntax: any (predicate {(v, k) ==> result})

Determines whether any element of a sequence exists or satisfies a condition. The enumeration of source is stopped as soon as the result can be determined.

public any ( callable | null $predicate = null ) : boolean
$predicate callable | null {(v, k) ==> result} A function to test each element for a condition. Default: null.
return boolean If predicate is null: true if the source sequence contains any elements; otherwise, false. If predicate is not null: true if any elements in the source sequence pass the test in the specified predicate; otherwise, false.

average() public method

Syntax: average ()

Computes the average of a sequence of numeric values.

Syntax: average (selector {(v, k) ==> result})

Computes the average of a sequence of numeric values that are obtained by invoking a transform function on each element of the input sequence.

public average ( callable | null $selector = null ) : number
$selector callable | null {(v, k) ==> result} A transform function to apply to each element. Default: value.
return number The average of the sequence of values.

call() public method

Syntax: process (action {(v, k) ==> void})

Process method does not start enumeration itself. To force enumeration, you can use {@link each} method.

Original LINQ method name: do.

public call ( callable $action ) : Enumerable
$action callable {(v, k) ==> void} The action to invoke for each element in the sequence.
return Enumerable The source sequence with the side-effecting behavior applied.

cast() public method

Syntax: cast (type)

The cast method causes an error if an element cannot be cast (exact error depends on the implementation of PHP casting), to get only elements of the specified type, use {@link ofType}.

public cast ( string $type ) : Enumerable
$type string The type to cast the elements to. Can be one of the built-in types: array, int (integer, long), float (real, double), null (unset), object, string.
return Enumerable An sequence that contains each element of the source sequence cast to the specified type.

contains() public method

Syntax: contains (value)

Determines whether a sequence contains a specified element. Enumeration is terminated as soon as a matching element is found.

public contains ( $value ) : boolean
$value mixed The value to locate in the sequence.
return boolean true if the source sequence contains an element that has the specified value; otherwise, false.

count() public method

Syntax: count ()

If source iterator implements {@link Countable}, that implementation is used to obtain the count of elements. Otherwise, this method determines the count.

Syntax: count (predicate {(v, k) ==> result})

Returns a number that represents how many elements in the specified sequence satisfy a condition.

public count ( callable | null $predicate = null ) : integer
$predicate callable | null {(v, k) ==> result} A function to test each element for a condition. Default: null.
return integer The number of elements in the input sequence.

distinct() public method

Element keys are values identifying elements. They are used as array keys and are subject to the same rules as array keys, for example, integer 100 and string "100" are considered equal.

Syntax: distinct ()

Returns distinct elements from a sequence using values as element keys.

Syntax: distinct (keySelector {(v, k) ==> value})

Returns distinct elements from a sequence using values produced by keySelector as element keys.

public distinct ( callable | null $keySelector = null ) : Enumerable
$keySelector callable | null {(v, k) ==> value} A function to extract the element key from each element. Default: value.
return Enumerable A sequence that contains distinct elements of the input sequence.

each() public method

Syntax: each (action {(v, k) ==> void})

Each method forces enumeration. To just add side-effect without enumerating, you can use {@link process} method.

Original LINQ method name: foreach.

public each ( callable $action = null )
$action callable {(v, k) ==> void} The action to invoke for each element in the sequence.

except() public method

Element keys are values identifying elements. They are used as array keys and are subject to the same rules as array keys, for example, integer 100 and string "100" are considered equal.

Syntax: distinct (other)

Produces the set difference of two sequences using values as element keys.

Syntax: distinct (other, keySelector {(v, k) ==> value})

Produces the set difference of two sequences using values produced by keySelector as element keys.

public except ( array | Iterator | IteratorAggregate | Enumerable $other, callable | null $keySelector = null ) : Enumerable
$other array | Iterator | IteratorAggregate | Enumerable A sequence whose elements that also occur in the source sequence will cause those elements to be removed from the returned sequence.
$keySelector callable | null {(v, k) ==> key} A function to extract the element key from each element. Default: value.
return Enumerable A sequence that contains the set difference of the elements of two sequences.

getIterator() public method

public getIterator ( ) : Iterator
return Iterator

groupBy() public method

Syntax: groupBy ()

Groups the elements of a sequence by its keys.

Syntax: groupBy (keySelector {(v, k) ==> key})

Groups the elements of a sequence according to a specified key selector function.

Syntax: groupBy (keySelector {(v, k) ==> key}, valueSelector {(v, k) ==> value})

Groups the elements of a sequence according to a specified key selector function and projects the elements for each group by using a specified function.

Syntax: groupBy (keySelector {(v, k) ==> key}, valueSelector {(v, k) ==> value}, resultSelectorValue {(e, k) ==> value} [, resultSelectorKey {(e, k) ==> key}])

Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key.

For all overloads except the last: the groupBy method returns a sequence of sequences, one inner sequence for each distinct key that was encountered. The outer sequence is yielded in an order based on the order of the elements in source that produced the first key of each inner sequence. Elements in a inner sequence are yielded in the order they appear in source.

public groupBy ( callable | null $keySelector = null, callable | null $valueSelector = null, callable | null $resultSelectorValue = null, callable | null $resultSelectorKey = null ) : Enumerable
$keySelector callable | null {(v, k) ==> key} A function to extract the key for each element. Default: key.
$valueSelector callable | null {(v, k) ==> value} A function to map each source element to a value in the inner sequence.
$resultSelectorValue callable | null {(e, k) ==> value} A function to create a result value from each group.
$resultSelectorKey callable | null {(e, k) ==> key} A function to create a result key from each group.
return Enumerable A sequence of sequences indexed by a key.

groupJoin() public method

Syntax: groupJoin (inner [, outerKeySelector {(v, k) ==> key} [, innerKeySelector {(v, k) ==> key} [, resultSelectorValue {(v, e, k) ==> value} [, resultSelectorKey {(v, e, k) ==> key}]]]])

GroupJoin produces hierarchical results, which means that elements from outer are paired with collections of matching elements from inner. GroupJoin enables you to base your results on a whole set of matches for each element of outer. If there are no correlated elements in inner for a given element of outer, the sequence of matches for that element will be empty but will still appear in the results.

The resultSelectorValue and resultSelectorKey functions are called only one time for each outer element together with a collection of all the inner elements that match the outer element. This differs from the {@link join} method, in which the result selector function is invoked on pairs that contain one element from outer and one element from inner. GroupJoin preserves the order of the elements of outer, and for each element of outer, the order of the matching elements from inner.

GroupJoin has no direct equivalent in traditional relational database terms. However, this method does implement a superset of inner joins and left outer joins. Both of these operations can be written in terms of a grouped join.

public groupJoin ( array | Iterator | IteratorAggregate | Enumerable $inner, callable | null $outerKeySelector = null, callable | null $innerKeySelector = null, callable | null $resultSelectorValue = null, callable | null $resultSelectorKey = null ) : Enumerable
$inner array | Iterator | IteratorAggregate | Enumerable The second (inner) sequence to join to the first (source, outer) sequence.
$outerKeySelector callable | null {(v, k) ==> key} A function to extract the join key from each element of the first sequence. Default: key.
$innerKeySelector callable | null {(v, k) ==> key} A function to extract the join key from each element of the second sequence. Default: key.
$resultSelectorValue callable | null {(v, e, k) ==> value} A function to create a result value from an element from the first sequence and a collection of matching elements from the second sequence. Default: {(v, e, k) ==> array(v, e)}.
$resultSelectorKey callable | null {(v, e, k) ==> key} A function to create a result key from an element from the first sequence and a collection of matching elements from the second sequence. Default: {(v, e, k) ==> k} (keys returned by outerKeySelector and innerKeySelector functions).
return Enumerable A sequence that contains elements that are obtained by performing a grouped join on two sequences.

intersect() public method

Element keys are values identifying elements. They are used as array keys and are subject to the same rules as array keys, for example, integer 100 and string "100" are considered equal.

Syntax: intersect (other)

Produces the set intersection of two sequences using values as element keys.

Syntax: intersect (other, keySelector {(v, k) ==> value})

Produces the set intersection of two sequences using values produced by keySelector as element keys.

public intersect ( array | Iterator | IteratorAggregate | Enumerable $other, callable | null $keySelector = null ) : Enumerable
$other array | Iterator | IteratorAggregate | Enumerable A sequence whose distinct elements that also appear in the first sequence will be returned.
$keySelector callable | null {(v, k) ==> key} A function to extract the element key from each element. Default: value.
return Enumerable A sequence that contains the elements that form the set intersection of two sequences.

join() public method

Syntax: join (inner [, outerKeySelector {(v, k) ==> key} [, innerKeySelector {(v, k) ==> key} [, resultSelectorValue {(v1, v2, k) ==> value} [, resultSelectorKey {(v1, v2, k) ==> key}]]]])

A join refers to the operation of correlating the elements of two sources of information based on a common key. Join brings the two information sources and the keys by which they are matched together in one method call. This differs from the use of {@link selectMany}, which requires more than one method call to perform the same operation.

Join preserves the order of the elements of the source, and for each of these elements, the order of the matching elements of inner.

In relational database terms, the Join method implements an inner equijoin. 'Inner' means that only elements that have a match in the other sequence are included in the results. An 'equijoin' is a join in which the keys are compared for equality. A left outer join operation has no dedicated standard query operator, but can be performed by using the {@link groupJoin} method.

public join ( array | Iterator | IteratorAggregate | Enumerable $inner, callable | null $outerKeySelector = null, callable | null $innerKeySelector = null, callable | null $resultSelectorValue = null, callable | null $resultSelectorKey = null ) : Enumerable
$inner array | Iterator | IteratorAggregate | Enumerable The sequence to join to the source sequence.
$outerKeySelector callable | null {(v, k) ==> key} A function to extract the join key from each element of the source sequence. Default: key.
$innerKeySelector callable | null {(v, k) ==> key} A function to extract the join key from each element of the second sequence. Default: key.
$resultSelectorValue callable | null {(v1, v2, k) ==> result} A function to create a result value from two matching elements. Default: {(v1, v2, k) ==> array(v1, v2)}.
$resultSelectorKey callable | null {(v1, v2, k) ==> result} A function to create a result key from two matching elements. Default: {(v1, v2, k) ==> k} (keys returned by outerKeySelector and innerKeySelector functions).
return Enumerable

max() public method

Syntax: max ()

Returns the maximum value in a sequence of values.

Syntax: max (selector {(v, k) ==> value})

Invokes a transform function on each element of a sequence and returns the maximum value.

public max ( callable | null $selector = null ) : number
$selector callable | null {(v, k) ==> value} A transform function to apply to each element. Default: value.
return number The maximum value in the sequence.

maxBy() public method

Syntax: maxBy (comparer {(a, b) ==> diff})

Returns the maximum value in a sequence of values, using specified comparer.

Syntax: maxBy (comparer {(a, b) ==> diff}, selector {(v, k) ==> value})

Invokes a transform function on each element of a sequence and returns the maximum value, using specified comparer.

public maxBy ( callable $comparer, callable | null $selector = null ) : number
$comparer callable {(a, b) ==> diff} Difference between a and b: <0 if a<b; 0 if a==b; >0 if a>b
$selector callable | null {(v, k) ==> value} A transform function to apply to each element. Default: value.
return number The maximum value in the sequence.

min() public method

Syntax: min ()

Returns the minimum value in a sequence of values.

Syntax: min (selector {(v, k) ==> value})

Invokes a transform function on each element of a sequence and returns the minimum value.

public min ( callable | null $selector = null ) : number
$selector callable | null {(v, k) ==> value} A transform function to apply to each element. Default: value.
return number The minimum value in the sequence.

minBy() public method

Syntax: minBy (comparer {(a, b) ==> diff})

Returns the minimum value in a sequence of values, using specified comparer.

Syntax: minBy (comparer {(a, b) ==> diff}, selector {(v, k) ==> value})

Invokes a transform function on each element of a sequence and returns the minimum value, using specified comparer.

public minBy ( callable $comparer, callable | null $selector = null ) : number
$comparer callable {(a, b) ==> diff} Difference between a and b: <0 if a<b; 0 if a==b; >0 if a>b
$selector callable | null {(v, k) ==> value} A transform function to apply to each element. Default: value.
return number The minimum value in the sequence.

ofType() public method

Syntax: ofType (type)

The ofType method returns only elements of the specified type. To instead receive an error if an element cannot be cast, use {@link cast}.

public ofType ( string $type ) : Enumerable
$type string The type to filter the elements of the sequence on. Can be either class name or one of the predefined types: array, int (integer, long), callable (callable), float (real, double), null, string, object, numeric, scalar.
return Enumerable A sequence that contains elements from the input sequence of the specified type.

orderBy() public method

Syntax: orderBy ([{(v, k) ==> key} [, {(a, b) ==> diff}]])

Three methods are defined to extend the type {@link OrderedEnumerable}, which is the return type of this method. These three methods, namely {@link OrderedEnumerable::thenBy thenBy}, {@link OrderedEnumerable::thenByDescending thenByDescending} and {@link OrderedEnumerable::thenByDir thenByDir}, enable you to specify additional sort criteria to sort a sequence. These methods also return an OrderedEnumerable, which means any number of consecutive calls to thenBy, thenByDescending or thenByDir can be made.

Because OrderedEnumerable inherits from Enumerable, you can call {@link orderBy}, {@link orderByDescending} or {@link orderByDir} on the results of a call to orderBy, orderByDescending, orderByDir, thenBy, thenByDescending or thenByDir. Doing this introduces a new primary ordering that ignores the previously established ordering.

This method performs an unstable sort; that is, if the keys of two elements are equal, the order of the elements is not preserved. In contrast, a stable sort preserves the order of elements that have the same key. Internally, {@link usort} is used.

public orderBy ( callable | null $keySelector = null, callable | integer | null $comparer = null ) : OrderedEnumerable
$keySelector callable | null {(v, k) ==> key} A function to extract a key from an element. Default: value.
$comparer callable | integer | null {(a, b) ==> diff} Difference between a and b: <0 if a<b; 0 if a==b; >0 if a>b. Can also be a combination of SORT_ flags.
return OrderedEnumerable

orderByDescending() public method

Syntax: orderByDescending ([{(v, k) ==> key} [, {(a, b) ==> diff}]])

Three methods are defined to extend the type {@link OrderedEnumerable}, which is the return type of this method. These three methods, namely {@link OrderedEnumerable::thenBy thenBy}, {@link OrderedEnumerable::thenByDescending thenByDescending} and {@link OrderedEnumerable::thenByDir thenByDir}, enable you to specify additional sort criteria to sort a sequence. These methods also return an OrderedEnumerable, which means any number of consecutive calls to thenBy, thenByDescending or thenByDir can be made.

Because OrderedEnumerable inherits from Enumerable, you can call {@link orderBy}, {@link orderByDescending} or {@link orderByDir} on the results of a call to orderBy, orderByDescending, orderByDir, thenBy, thenByDescending or thenByDir. Doing this introduces a new primary ordering that ignores the previously established ordering.

This method performs an unstable sort; that is, if the keys of two elements are equal, the order of the elements is not preserved. In contrast, a stable sort preserves the order of elements that have the same key. Internally, {@link usort} is used.

public orderByDescending ( callable | null $keySelector = null, callable | integer | null $comparer = null ) : OrderedEnumerable
$keySelector callable | null {(v, k) ==> key} A function to extract a key from an element. Default: value.
$comparer callable | integer | null {(a, b) ==> diff} Difference between a and b: <0 if a<b; 0 if a==b; >0 if a>b. Can also be a combination of SORT_ flags.
return OrderedEnumerable

orderByDir() public method

Syntax: orderByDir (false|true [, {(v, k) ==> key} [, {(a, b) ==> diff}]])

Three methods are defined to extend the type {@link OrderedEnumerable}, which is the return type of this method. These three methods, namely {@link OrderedEnumerable::thenBy thenBy}, {@link OrderedEnumerable::thenByDescending thenByDescending} and {@link OrderedEnumerable::thenByDir thenByDir}, enable you to specify additional sort criteria to sort a sequence. These methods also return an OrderedEnumerable, which means any number of consecutive calls to thenBy, thenByDescending or thenByDir can be made.

Because OrderedEnumerable inherits from Enumerable, you can call {@link orderBy}, {@link orderByDescending} or {@link orderByDir} on the results of a call to orderBy, orderByDescending, orderByDir, thenBy, thenByDescending or thenByDir. Doing this introduces a new primary ordering that ignores the previously established ordering.

This method performs an unstable sort; that is, if the keys of two elements are equal, the order of the elements is not preserved. In contrast, a stable sort preserves the order of elements that have the same key. Internally, {@link usort} is used.

public orderByDir ( integer | boolean $sortOrder, callable | null $keySelector = null, callable | integer | null $comparer = null ) : OrderedEnumerable
$sortOrder integer | boolean A direction in which to order the elements: false or SORT_DESC for ascending (by increasing value), true or SORT_ASC for descending (by decreasing value).
$keySelector callable | null {(v, k) ==> key} A function to extract a key from an element. Default: value.
$comparer callable | integer | null {(a, b) ==> diff} Difference between a and b: <0 if a<b; 0 if a==b; >0 if a>b. Can also be a combination of SORT_ flags.
return OrderedEnumerable

select() public method

Syntax: select (selectorValue {(v, k) ==> result} [, selectorKey {(v, k) ==> result}])

This projection method requires the transform functions, selectorValue and selectorKey, to produce one key-value pair for each value in the source sequence. If selectorValue returns a value that is itself a collection, it is up to the consumer to traverse the subsequences manually. In such a situation, it might be better for your query to return a single coalesced sequence of values. To achieve this, use the {@link selectMany()} method instead of select. Although selectMany works similarly to select, it differs in that the transform function returns a collection that is then expanded by selectMany before it is returned.

public select ( callable $selectorValue, callable | null $selectorKey = null ) : Enumerable
$selectorValue callable {(v, k) ==> value} A transform function to apply to each value.
$selectorKey callable | null {(v, k) ==> key} A transform function to apply to each key. Default: key.
return Enumerable A sequence whose elements are the result of invoking the transform functions on each element of source.

selectMany() public method

Syntax: selectMany ()

The selectMany method enumerates the input sequence, where each element is a sequence, and then enumerates and yields the elements of each such sequence. That is, for each element of source, selectorValue and selectorKey are invoked and a sequence of key-value pairs is returned. selectMany then flattens this two-dimensional collection of collections into a one-dimensional sequence and returns it. For example, if a query uses selectMany to obtain the orders for each customer in a database, the result is a sequence of orders. If instead the query uses {@link select} to obtain the orders, the collection of collections of orders is not combined and the result is a sequence of sequences of orders.

Syntax: selectMany (collectionSelector {(v, k) ==> enum})

The selectMany method enumerates the input sequence, uses transform functions to map each element to a sequence, and then enumerates and yields the elements of each such sequence.

Syntax: selectMany (collectionSelector {(v, k) ==> enum} [, resultSelectorValue {(v, k1, k2) ==> value} [, resultSelectorKey {(v, k1, k2) ==> key}]])

Projects each element of a sequence to a sequence, flattens the resulting sequences into one sequence, and invokes a result selector functions on each element therein.

The selectMany method is useful when you have to keep the elements of source in scope for query logic that occurs after the call to selectMany. If there is a bidirectional relationship between objects in the source sequence and objects returned from collectionSelector, that is, if a sequence returned from collectionSelector provides a property to retrieve the object that produced it, you do not need this overload of selectMany. Instead, you can use simpler selectMany overload and navigate back to the source object through the returned sequence.

public selectMany ( callable $collectionSelector = null, callable | null $resultSelectorValue = null, callable | null $resultSelectorKey = null ) : Enumerable
$collectionSelector callable {(v, k) ==> enum} A transform function to apply to each element.
$resultSelectorValue callable | null {(v, k1, k2) ==> value} A transform function to apply to each value of the intermediate sequence. Default: {(v, k1, k2) ==> v}.
$resultSelectorKey callable | null {(v, k1, k2) ==> key} A transform function to apply to each key of the intermediate sequence. Default: increment.
return Enumerable A sequence whose elements are the result of invoking the one-to-many transform function on each element of the input sequence.

sum() public method

Syntax: sum ()

Computes the sum of a sequence of values.

Syntax: sum (selector {(v, k) ==> result})

Computes the sum of the sequence of values that are obtained by invoking a transform function on each element of the input sequence.

This method returns zero if source contains no elements.

public sum ( callable | null $selector = null ) : number
$selector callable | null {(v, k) ==> result} A transform function to apply to each element.
return number The sum of the values in the sequence.

toArray() public method

Syntax: toArray ()

The toArray method forces immediate query evaluation and returns an array that contains the query results.

The toArray method does not traverse into elements of the sequence, only the sequence itself is converted. That is, if elements of the sequence are {@link Traversable} or arrays containing Traversable values, they will remain as is. To traverse deeply, you can use {@link toArrayDeep} method.

Keys from the sequence are preserved. If the source sequence contains multiple values with the same key, the result array will only contain the latter value. To discard keys, you can use {@link toList} method. To preserve all values and keys, you can use {@link toLookup} method.

public toArray ( ) : array
return array An array that contains the elements from the input sequence.

toArrayDeep() public method

Syntax: toArrayDeep ()

The toArrayDeep method forces immediate query evaluation and returns an array that contains the query results.

The toArrayDeep method traverses into elements of the sequence. That is, if elements of the sequence are {@link Traversable} or arrays containing Traversable values, they will be converted to arrays too. To convert only the sequence itself, you can use {@link toArray} method.

Keys from the sequence are preserved. If the source sequence contains multiple values with the same key, the result array will only contain the latter value. To discard keys, you can use {@link toListDeep} method. To preserve all values and keys, you can use {@link toLookup} method.

public toArrayDeep ( ) : array
return array An array that contains the elements from the input sequence.

toArrayDeepProc() protected method

Proc for {@link toArrayDeep}.
protected toArrayDeepProc ( $enum ) : array
$enum \Traversable Source sequence.
return array An array that contains the elements from the input sequence.

toDictionary() public method

Syntax: toDictionary ([keySelector {(v, k) ==> key} [, valueSelector {(v, k) ==> value}]])

The toDictionary method returns an array, a one-to-one dictionary that maps keys to values. If the source sequence contains multiple values with the same key, the result array will only contain the latter value.

public toDictionary ( callable | null $keySelector = null, callable | null $valueSelector = null ) : array
$keySelector callable | null {(v, k) ==> key} A function to extract a key from each element. Default: key.
$valueSelector callable | null {(v, k) ==> value} A transform function to produce a result value from each element. Default: value.
return array An array that contains keys and values selected from the input sequence.

toJSON() public method

Syntax: toJSON ([options])

This function only works with UTF-8 encoded data.

See also: json_encode
public toJSON ( integer $options ) : string
$options integer Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_UNESCAPED_UNICODE. Default: 0.
return string A JSON encoded string on success or false on failure.

toKeys() public method

Syntax: toKeys ()

See also: array_keys
public toKeys ( ) : Enumerable
return Enumerable A sequence with keys from the source sequence as values and sequental integers as keys.

toList() public method

Syntax: toList ()

The toList method forces immediate query evaluation and returns an array that contains the query results.

The toList method does not traverse into elements of the sequence, only the sequence itself is converted. That is, if elements of the sequence are {@link Traversable} or arrays containing Traversable values, they will remain as is. To traverse deeply, you can use {@link toListDeep} method.

Keys from the sequence are discarded. To preserve keys and lose values with the same keys, you can use {@link toArray} method. To preserve all values and keys, you can use {@link toLookup} method.

public toList ( ) : array
return array An array that contains the elements from the input sequence.

toListDeep() public method

Syntax: toListDeep ()

The toListDeep method forces immediate query evaluation and returns an array that contains the query results.

The toListDeep method traverses into elements of the sequence. That is, if elements of the sequence are {@link Traversable} or arrays containing Traversable values, they will be converted to arrays too. To convert only the sequence itself, you can use {@link toList} method.

Keys from the sequence are discarded. To preserve keys and lose values with the same keys, you can use {@link toArrayDeep} method. To preserve all values and keys, you can use {@link toLookup} method.

public toListDeep ( ) : array
return array An array that contains the elements from the input sequence.

toListDeepProc() protected method

Proc for {@link toListDeep}.
protected toListDeepProc ( $enum ) : array
$enum \Traversable Source sequence.
return array An array that contains the elements from the input sequence.

toLookup() public method

Syntax: toLookup ([keySelector {(v, k) ==> key} [, valueSelector {(v, k) ==> value}]])

The toLookup method returns an array, a one-to-many dictionary that maps keys to arrays of values.

public toLookup ( callable | null $keySelector = null, callable | null $valueSelector = null ) : array
$keySelector callable | null {(v, k) ==> key} A function to extract a key from each element. Default: key.
$valueSelector callable | null {(v, k) ==> value} A transform function to produce a result value from each element. Default: value.
return array An array that contains keys and value arrays selected from the input sequence.

toObject() public method

Syntax: toObject ([propertySelector {(v, k) ==> name} [, valueSelector {(v, k) ==> value}]])

public toObject ( callable | null $propertySelector = null, callable | null $valueSelector = null ) : stdClass
$propertySelector callable | null {(v, k) ==> name} A function to extract a property name from an element. Must return a valid PHP identifier. Default: key.
$valueSelector callable | null {(v, k) ==> value} A function to extract a property value from an element. Default: value.
return stdClass

toString() public method

Syntax: toString ([separator [, selector]])

See also: implode
public toString ( string $separator = '', callable | null $valueSelector = null ) : string
$separator string A string separating values in the result string. Default: ''.
$valueSelector callable | null {(v, k) ==> value} A transform function to apply to each element. Default: value.
return string

toValues() public method

Syntax: toValues ()

See also: array_values
public toValues ( ) : Enumerable
return Enumerable A sequence with the same values and sequental integers as keys.

tryGetArrayCopy() protected method

protected tryGetArrayCopy ( )

union() public method

Element keys are values identifying elements. They are used as array keys and are subject to the same rules as array keys, for example, integer 100 and string "100" are considered equal.

This method excludes duplicates from the return set. This is different behavior to the {@link concat} method, which returns all the elements in the input sequences including duplicates.

Syntax: union (other)

Produces the set union of two sequences using values as element keys.

Syntax: union (other, keySelector {(v, k) ==> value})

Produces the set union of two sequences using values produced by keySelector as element keys.

public union ( array | Iterator | IteratorAggregate | Enumerable $other, callable | null $keySelector = null ) : Enumerable
$other array | Iterator | IteratorAggregate | Enumerable A sequence whose distinct elements form the second set for the union.
$keySelector callable | null {(v, k) ==> key} A function to extract the element key from each element. Default: value.
return Enumerable A sequence that contains the elements from both input sequences, excluding duplicates.

where() public method

Syntax: where (predicate {(v, k) ==> result})

public where ( callable $predicate ) : Enumerable
$predicate callable {(v, k) ==> result} A function to test each element for a condition.
return Enumerable A sequence that contains elements from the input sequence that satisfy the condition.

write() public method

Syntax: write ([separator [, selector]])

public write ( string $separator = '', callable | null $selector = null )
$separator string A string separating values in the result string. Default: ''.
$selector callable | null {(v, k) ==> value} A transform function to apply to each element. Default: value.

writeLine() public method

Syntax: writeLine ([selector])

public writeLine ( callable | null $selector = null ) : string
$selector callable | null {(v, k) ==> value} A transform function to apply to each element. Default: value.
return string