List_Util
in package
List utility.
Utility class to handle operations on an array of objects.
Table of Contents
- $input : array<string|int, mixed>
- The input array.
- $orderby : array<string|int, mixed>
- Temporary arguments for sorting.
- $output : array<string|int, mixed>
- The output array.
- __construct() : mixed
- Constructor.
- filter() : array<string|int, mixed>
- Filters the list, based on a set of key => value arguments.
- get_input() : array<string|int, mixed>
- Returns the original input array.
- get_output() : array<string|int, mixed>
- Returns the output array.
- pluck() : array<string|int, mixed>
- Plucks a certain field out of each object in the list.
- sort() : array<string|int, mixed>
- Sorts the list, based on one or more orderby arguments.
- sort_callback() : int
- Callback to sort the list by specific fields.
Properties
$input
The input array.
private
array<string|int, mixed>
$input
= array()
Tags
$orderby
Temporary arguments for sorting.
private
array<string|int, mixed>
$orderby
= array()
Tags
$output
The output array.
private
array<string|int, mixed>
$output
= array()
Tags
Methods
__construct()
Constructor.
public
__construct(array<string|int, mixed> $input) : mixed
Sets the input array.
Parameters
- $input : array<string|int, mixed>
-
Array to perform operations on.
Return values
mixed —filter()
Filters the list, based on a set of key => value arguments.
public
filter([array<string|int, mixed> $args = array() ][, string $operator = 'AND' ]) : array<string|int, mixed>
Parameters
- $args : array<string|int, mixed> = array()
-
Optional. An array of key => value arguments to match against each object. Default empty array.
- $operator : string = 'AND'
-
Optional. The logical operation to perform. 'AND' means all elements from the array must match. 'OR' means only one element needs to match. 'NOT' means no elements may match. Default 'AND'.
Return values
array<string|int, mixed> —Array of found values.
get_input()
Returns the original input array.
public
get_input() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —The input array.
get_output()
Returns the output array.
public
get_output() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —The output array.
pluck()
Plucks a certain field out of each object in the list.
public
pluck(int|string $field[, int|string $index_key = null ]) : array<string|int, mixed>
This has the same functionality and prototype of array_column() (PHP 5.5) but also supports objects.
Parameters
- $field : int|string
-
Field from the object to place instead of the entire object
- $index_key : int|string = null
-
Optional. Field from the object to use as keys for the new array. Default null.
Return values
array<string|int, mixed> —Array of found values. If $index_key is set, an array of found values with keys
corresponding to $index_key. If $index_key is null, array keys from the original
$list will be preserved in the results.
sort()
Sorts the list, based on one or more orderby arguments.
public
sort([string|array<string|int, mixed> $orderby = array() ][, string $order = 'ASC' ][, bool $preserve_keys = false ]) : array<string|int, mixed>
Parameters
- $orderby : string|array<string|int, mixed> = array()
-
Optional. Either the field name to order by or an array of multiple orderby fields as $orderby => $order.
- $order : string = 'ASC'
-
Optional. Either 'ASC' or 'DESC'. Only used if $orderby is a string.
- $preserve_keys : bool = false
-
Optional. Whether to preserve keys. Default false.
Return values
array<string|int, mixed> —The sorted array.
sort_callback()
Callback to sort the list by specific fields.
private
sort_callback(object|array<string|int, mixed> $a, object|array<string|int, mixed> $b) : int
Parameters
- $a : object|array<string|int, mixed>
-
One object to compare.
- $b : object|array<string|int, mixed>
-
The other object to compare.
Tags
Return values
int —0 if both objects equal. -1 if second object should come first, 1 otherwise.