PHP Class VersionPress\Utils\ArrayUtils

ファイルを表示 Open project: versionpress/versionpress Class Usage Examples

Public Methods

Method Description
all ( array $array, callable $predicate ) : boolean Determines whether all elements of an array satisfy a condition.
any ( array $array, callable $predicate ) : boolean Determines whether any element of an array satisfies a condition.
isAssociative ( mixed $array ) : boolean Returns true if the array is associative. Associative being defined as an array containing at least one string key.
map ( callable $mapFn, array $array ) : array Extends array_map function with keys.
mapreduce ( $data, $mapFn, $reduceFn ) : array Transforms and groups data in array $mapFn emits key and value. These values are grouped by their keys and passed into $reduceFn which can transform the group and emit result.
parametrize ( $array )
stablesort ( &$array, string $cmp_function = 'strcmp' ) Similar to usort() but maintains order of equal items (the algorithm is "stable").
unique ( array $array, callable | null $fn = null ) : array Removes duplicate values from an array.

Method Details

all() public static method

Determines whether all elements of an array satisfy a condition.
public static all ( array $array, callable $predicate ) : boolean
$array array
$predicate callable
return boolean

any() public static method

Determines whether any element of an array satisfies a condition.
public static any ( array $array, callable $predicate ) : boolean
$array array
$predicate callable
return boolean

isAssociative() public static method

Returns true if the array is associative. Associative being defined as an array containing at least one string key.
public static isAssociative ( mixed $array ) : boolean
$array mixed Array is generally expected but any value may be provided (will return false)
return boolean

map() public static method

The $mapFn takes two params - $value and $key.
public static map ( callable $mapFn, array $array ) : array
$mapFn callable
$array array
return array

mapreduce() public static method

$mapFn has signature ($item, $mapEmit) where $item is item from data and $mapEmit is emit function $reduceFn has signature ($key, $items, $reduceEmit) where $key is the key the data are grouped by, $items is the group and $reduce emit is an emit function $mapEmit has signature ($key, $value) where $key is the key the data are grouped by and $value is a transformed item $reduceEmit has signature ($obj) where $obj is the transformed group
public static mapreduce ( $data, $mapFn, $reduceFn ) : array
$data
$mapFn
$reduceFn
return array

parametrize() public static method

public static parametrize ( $array )

stablesort() public static method

Adapted from http://stackoverflow.com/a/4353844/21728 / http://php.net/manual/en/function.usort.php#38827
public static stablesort ( &$array, string $cmp_function = 'strcmp' )
$array
$cmp_function string

unique() public static method

Uses custom function to map the values to a comparable value. It preserves keys.
public static unique ( array $array, callable | null $fn = null ) : array
$array array
$fn callable | null
return array