11use
function array_flip;
12use
function array_key_exists;
13use
function array_keys;
14use
function array_slice;
45 foreach ( $array as $elt ) {
46 $hashes[$elt] = md5( $elt . $separator . $key );
48 uasort( $array,
static function ( $a, $b ) use ( $hashes ) {
49 return strcmp( $hashes[$a], $hashes[$b] );
61 if ( !is_array( $weights ) || count( $weights ) == 0 ) {
65 $sum = array_sum( $weights );
67 # No loads on any of them
68 # In previous versions, this triggered an unweighted random selection,
69 # but this feature has been removed as of April 2006 to allow for strict
70 # separation of query groups.
73 $max = mt_getrandmax();
74 $rand = mt_rand( 0, $max ) / $max * $sum;
77 foreach ( $weights as $i => $w ) {
79 # Do not return keys if they have 0 weight.
80 # Note that the "all 0 weight" case is handed above
81 if ( $w > 0 && $sum >= $rand ) {
107 $comparisonCallback, $target
109 if ( $valueCount === 0 ) {
116 $mid = $min + ( ( $max - $min ) >> 1 );
117 $item = $valueCallback( $mid );
118 $comparison = $comparisonCallback( $target, $item );
119 if ( $comparison > 0 ) {
121 } elseif ( $comparison == 0 ) {
127 }
while ( $min < $max - 1 );
130 $item = $valueCallback( $min );
131 $comparison = $comparisonCallback( $target, $item );
132 if ( $comparison < 0 ) {
155 foreach ( $array1 as $key => $value ) {
156 if ( is_array( $value ) ) {
158 foreach ( $arrays as $array ) {
159 if ( isset( $array[$key] ) ) {
160 $args[] = $array[$key];
164 if ( count( $valueret ) ) {
165 $ret[$key] = $valueret;
168 foreach ( $arrays as $array ) {
169 if ( isset( $array[$key] ) && $array[$key] === $value ) {
205 $numInputs = count( $inputArrays );
206 if ( $numInputs === 0 ) {
211 foreach ( $inputArrays as &$inputArray ) {
212 if ( !count( $inputArray ) ) {
215 reset( $inputArray );
217 unset( $inputArray );
224 foreach ( $inputArrays as $inputArray ) {
225 $element[] = current( $inputArray );
227 $outputArrays[] = $element;
234 for ( $paramIndex = $numInputs - 1; $paramIndex >= 0; $paramIndex-- ) {
235 next( $inputArrays[$paramIndex] );
236 if ( key( $inputArrays[$paramIndex] ) ===
null ) {
237 reset( $inputArrays[$paramIndex] );
245 return $outputArrays;
260 public static function arrayPlus2d( array $baseArray, array $newValues ): array {
262 foreach ( $baseArray as $name => &$groupVal ) {
263 if ( isset( $newValues[$name] ) ) {
264 $groupVal += $newValues[$name];
268 $baseArray += $newValues;
283 public static function insertAfter( array $array, array $insert,
string|
int $after ): array {
285 $keys = array_keys( $array );
286 $offsetByKey = array_flip( $keys );
288 if ( !array_key_exists( $after, $offsetByKey ) ) {
291 $offset = $offsetByKey[$after];
294 $before = array_slice( $array, 0, $offset + 1,
true );
295 $after = array_slice( $array, $offset + 1, count( $array ) - $offset,
true );
297 $output = $before + $insert + $after;
304class_alias( ArrayUtils::class,
'ArrayUtils' );