MediaWiki  master
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. More...
 
static consistentHashSort (&$array, $key, $separator="\0")
 Sort the given array in a pseudo-random order which depends only on the given key and each element value in $array. More...
 
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. More...
 
static pickRandom ( $weights)
 Given an array of non-normalised probabilities, this function will select an element and return the appropriate key. More...
 

Detailed Description

A collection of static methods to play with arrays.

Since
1.21

Definition at line 28 of file ArrayUtils.php.

Member Function Documentation

◆ arrayDiffAssocRecursive()

static 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 158 of file ArrayUtils.php.

References $args.

◆ consistentHashSort()

static 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 49 of file ArrayUtils.php.

References $hashes.

Referenced by PoolCounterRedis\getConnection(), RedisBagOStuff\getConnection(), and SqlBagOStuff\getTableByKey().

◆ findLowerBound()

static 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().
string$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 112 of file ArrayUtils.php.

Referenced by IcuCollation\getFirstLetter().

◆ pickRandom()

static 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
bool|int|string

Definition at line 66 of file ArrayUtils.php.

Referenced by JobQueueFederated\doPop(), Wikimedia\Rdbms\LoadBalancer\getRandomNonLagged(), Wikimedia\Rdbms\LoadBalancer\pickReaderIndex(), JobQueueFederated\tryJobInsertions(), and Wikimedia\Rdbms\LoadBalancer\waitForOne().


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