MediaWiki master
Wikimedia\ArrayUtils\ArrayUtils Class Reference

A collection of static methods to play with arrays. More...

Static Public Member Functions

static arrayDiffAssocRecursive ( $array1,... $arrays)
 Do array_diff_assoc() on multi-dimensional arrays.
 
static arrayPlus2d (array $baseArray, array $newValues)
 Merges two (possibly) 2 dimensional arrays into the target array ($baseArray).
 
static cartesianProduct (... $inputArrays)
 Make an array consisting of every combination of the elements of the input arrays.
 
static consistentHashSort (&$array, $key, $separator="\000")
 Sort the given array in a pseudo-random order which depends only on the given key and each element value in $array.
 
static findLowerBound ( $valueCallback, $valueCount, $comparisonCallback, $target)
 Do a binary search, and return the index of the largest item that sorts less than or equal to the target value.
 
static insertAfter (array $array, array $insert, string|int $after)
 Insert an array into another array after the specified key.
 
static pickRandom ( $weights)
 Given an array of non-normalised probabilities, this function will select an element and return the appropriate key.
 

Detailed Description

A collection of static methods to play with arrays.

Since
1.21

Definition at line 22 of file ArrayUtils.php.

Member Function Documentation

◆ arrayDiffAssocRecursive()

static Wikimedia\ArrayUtils\ArrayUtils::arrayDiffAssocRecursive ( $array1,
$arrays )
static

Do array_diff_assoc() on multi-dimensional arrays.

Note: empty arrays are removed.

Since
1.23
Parameters
array$array1The array to compare from
array...$arrays More arrays to compare against
Returns
array An array containing all the values from array1 that are not present in any of the other arrays.

Definition at line 152 of file ArrayUtils.php.

References Wikimedia\ArrayUtils\ArrayUtils\arrayDiffAssocRecursive().

Referenced by Wikimedia\ArrayUtils\ArrayUtils\arrayDiffAssocRecursive().

◆ arrayPlus2d()

static Wikimedia\ArrayUtils\ArrayUtils::arrayPlus2d ( array $baseArray,
array $newValues )
static

Merges two (possibly) 2 dimensional arrays into the target array ($baseArray).

Values that exist in both values will be combined with += (all values of the array of $newValues will be added to the values of the array of $baseArray, while values, that exists in both, the value of $baseArray will be used).

Parameters
array$baseArrayThe array where you want to add the values of $newValues to
array$newValuesAn array with new values
Returns
array The combined array
Since
1.46

Definition at line 260 of file ArrayUtils.php.

◆ cartesianProduct()

static Wikimedia\ArrayUtils\ArrayUtils::cartesianProduct ( $inputArrays)
static

Make an array consisting of every combination of the elements of the input arrays.

Each element of the output array is an array with a number of elements equal to the number of input parameters.

In mathematical terms, this is an n-ary Cartesian product.

For example, ArrayUtils::cartesianProduct( [ 1, 2 ], [ 'a', 'b' ] ) produces [ [ 1, 'a' ], [ 1, 'b' ], [ 2, 'a' ], [ 2, 'b' ] ]

If any of the input arrays is empty, the result is the empty array []. This is in keeping with the mathematical definition.

If no parameters are given, the result is also the empty array.

The array keys are ignored. This implementation uses the internal pointers of the input arrays to keep track of the current position without referring to the keys.

Since
1.35
Parameters
array...$inputArrays
Returns
array

Definition at line 204 of file ArrayUtils.php.

◆ consistentHashSort()

static Wikimedia\ArrayUtils\ArrayUtils::consistentHashSort ( & $array,
$key,
$separator = "\000" )
static

Sort the given array in a pseudo-random order which depends only on the given key and each element value in $array.

This is typically used for load balancing between servers each with a local cache.

Keys are preserved. The input array is modified in place.

Note: Benchmarking on PHP 5.3 and 5.4 indicates that for small strings, md5() is only 10% slower than hash('joaat',...) etc., since the function call overhead dominates. So there's not much justification for breaking compatibility with installations compiled with ./configure –disable-hash.

Parameters
array&$arrayArray to sort
string$key
string$separatorA separator used to delimit the array elements and the key. This can be chosen to provide backwards compatibility with various consistent hash implementations that existed before this function was introduced.

Definition at line 43 of file ArrayUtils.php.

◆ findLowerBound()

static Wikimedia\ArrayUtils\ArrayUtils::findLowerBound ( $valueCallback,
$valueCount,
$comparisonCallback,
$target )
static

Do a binary search, and return the index of the largest item that sorts less than or equal to the target value.

Since
1.23
Parameters
callable$valueCallbackA function to call to get the value with a given array index.
int$valueCountThe number of items accessible via $valueCallback, indexed from 0 to $valueCount - 1
callable$comparisonCallbackA callback to compare two values, returning -1, 0 or 1 in the style of strcmp().
mixed$targetThe target value to find.
Returns
int|bool The item index of the lower bound, or false if the target value sorts before all items.

Definition at line 106 of file ArrayUtils.php.

◆ insertAfter()

static Wikimedia\ArrayUtils\ArrayUtils::insertAfter ( array $array,
array $insert,
string|int $after )
static

Insert an array into another array after the specified key.

If the key is not present in the input array, it is returned without modification.

Parameters
array$array
array$insertThe array to insert.
string | int$afterThe key to insert after.
Returns
array
Since
1.46

Definition at line 283 of file ArrayUtils.php.

Referenced by MediaWiki\Api\ApiComparePages\getAllowedParams().

◆ pickRandom()

static Wikimedia\ArrayUtils\ArrayUtils::pickRandom ( $weights)
static

Given an array of non-normalised probabilities, this function will select an element and return the appropriate key.

Parameters
array$weights
Returns
int|string|false

Definition at line 60 of file ArrayUtils.php.


The documentation for this class was generated from the following file: