17 public static function explode($array,$delimiter = null,$separator = null){
18 if(is_array($array))
return $array;
19 if($array === null)
return [];
20 if(!$delimiter || !is_string($array))
return [$array];
22 if($array)
foreach(explode($delimiter,$array) as $value){
24 $data = explode($separator,$value,2);
25 $value = array_pop($data);
26 $key = array_shift($data);
28 else $key = count($result);
29 $result[$key] = $value;
40 public static function implode($array,$delimiter = null,$separator = null){
41 if(!is_array($array))
return $array;
42 if($separator)
foreach($array as $key => &$value) $value = $key . $separator . $value;
44 return implode($delimiter,$array);
53 public static function exists($array,$key){
54 if(!is_array($key)) $key = [$key];
55 elseif(!$key)
return false;
57 if(!is_array($array) || !array_key_exists($sub = array_shift($key),$array))
return false;
58 $array = $array[$sub];
70 public static function get($array,$key,$default = null){
71 if(!$array || !is_array($array))
return $default;
72 if(!is_array($key))
return array_key_exists($key,$array) ? $array[$key] : $default;
73 while($key)
if(!array_key_exists($sub = array_shift($key),$array) || (!is_array($array = $array[$sub]) && $key))
return $default;
85 public static function iget($array,$key,$default = null){
86 if(!is_array($key)) $key = [$key];
87 elseif(!$key)
return $default;
89 if(!is_array($array))
return $default;
90 if(!array_key_exists($sub = array_shift($key),$array)){
92 foreach($array as $index => $value)
if($found = !strcasecmp($index,$sub)){
96 if(!$found)
return $default;
98 $array = $array[$sub];
110 public static function set(&$array,$key,$value){
111 if(is_array($keys = $key)) $key = array_shift($keys);
114 if(!array_key_exists($key,$array)) $array[$key] = [];
115 self::set($array[$key],$keys,$value);
117 else $array[$key] = $value;
126 public static function add(&$array,$key,$value = null){
127 if(is_array($keys = $key)) $key = array_shift($keys);
129 if(!array_key_exists($key,$array)) $array[$key] = $keys ? [] : $value;
130 if($keys) self::add($array[$key],$keys,$value);
137 public static function delete(&$array,$key){
138 if(is_array($keys = $key)){
139 $key = array_pop($keys);
140 foreach($keys as $sub)
141 if(array_key_exists($sub,$array)) $array = &$array[$sub];
153 public static function flatten($array,$key_glue = null,$key_prefix = null){
155 foreach($array as $key => $value)
156 if(is_array($value)) $result = array_merge($result,self::flatten($value,$key_glue,$key_prefix . $key . $key_glue));
157 else $result[$key_glue ? $key_prefix . $key : count($result)] = $value;
166 public static function expand($array,$key_glue){
168 foreach($array as $key => $value) self::set($result,explode($key_glue,$key),$value);
178 public static function value($array,$index = 0,$default = null){
179 return self::get(array_values($array),$index + ($index < 0 ? count($array) : 0),$default);
187 public static function key($array,$index = 0){
188 return self::get(array_keys($array),$index + ($index < 0 ? count($array) : 0),
false);
195 public static function assoc($array){
196 return array_values($array) !== $array;
206 public static function splice($array,$offset,$length = 0,$replace = null){
207 if($offset < 0) $offset += count($array);
208 return array_merge(array_slice($array,0,$offset),$replace ?: [],array_slice($array,$offset + $length));
216 public static function merge(...$arrays){
218 foreach($arrays as $array)
if($result){
219 $insert = count($keys = array_keys($result));
220 foreach($keys as $index => $key)
if(array_key_exists($key,$array)){
224 foreach($array as $key => $value)
if(in_array($key,$keys)){
225 $result[$key] = $value;
226 $insert = array_search($key,array_keys($result)) + 1;
228 else $result = self::splice($result,$insert++,0,[$key => $value]);
230 else $result = $array;
240 public static function resize($array,$length,$filler = null){
241 return array_slice(array_pad($array,$length,$filler),0,$length);
251 public static function combine($keys,$values,$filler = null){
252 return array_combine($keys,self::resize($values,count($keys),$filler));
261 public static function isearch($array,$value){
262 if(($key = array_search($value,$array)) !==
false)
return $key;
263 $value = mb_strtolower($value);
264 foreach($array as $key => $sub)
if(mb_strtolower($sub) == $value)
return $key;
273 public static function prefix($array,$prefix){
274 foreach($array as &$value) $value = $prefix . $value;
286 foreach($array as $key => $value) $result[$prefix . $key] = $value;
296 public static function changeKey($array,$keys,$keep =
true){
298 foreach($array as $key => $value)
299 if(array_key_exists($key,$keys)) $result[$keys[$key]] = $value;
300 elseif($keep) $result[$key] = $value;
311 public static function mergeKey($array,$key_name = null,$value_name = null){
313 foreach($array as $key => $value){
315 if($key_name) $record[$key_name] = $key;
316 if($value_name) $record[$value_name] = $value;
317 elseif(is_array($value)) $record += $value;
318 $result[$key] = $record;
329 public static function column($array,$column){
331 foreach($array as $key => $record) $result[$key] = self::get($record,$column);
340 public static function group($array,$column){
342 foreach($array as $key => $record){
343 if(!array_key_exists($group = self::get($record,$column),$result)) $result[$group] = [];
344 $result[$group][$key] = $record;
356 $keys = array_keys($array);
358 foreach($keys as $key) $result[$key] = $array[$key];
367 return ($count = count($array)) ? array_sum($array) / $count :
false;
375 public static function deviation($array, &$average = null){
376 $average = self::average($array);
378 foreach($array as $value) $total += pow($value - $average,2);
379 return $array ? sqrt($total / count($array)) :
false;
387 public static function select($array,$keys){
389 foreach($array as $key => $value)
if(in_array($key,$keys)) $result[$key] = $value;
399 public static function sort($array,$options = null,$key =
false){
400 if(is_callable($options)){
401 if($key) uksort($array,$options);
402 else uasort($array,$options);
405 if($key) ksort($array,$options);
406 else asort($array,$options);
419 $previous = $start = array_shift($array);
421 $current = array_shift($array);
422 if($current > $previous + 1){
423 $result[$start] = $previous;
426 $previous = $current;
428 $result[$start] = $previous;
439 public static function groupByLetter($array,$length = 1,$case_sensitive =
false){
441 foreach($array as $key => $value){
442 $letter = substr($value,0,$length);
443 if(!$case_sensitive) $letter = mb_convert_case($letter,MB_CASE_UPPER);
444 if(!array_key_exists($letter,$result)) $result[$letter] = [];
445 $result[$letter][$key] = $value;
459 public static function groupByStart($array,$max = 25,$other = 0,$min =
true,$word =
'\\W+\\w*'){
460 $other = ($other ===
true) ? floor(count($array) / $max / 2) : min($other,count($array));
461 $min = max(2,($min ===
true) ? floor(count($array) / $max / 10) : $min);
462 $result = $others = [];
463 foreach($array as $key => $value){
464 if(!array_key_exists($value,$result)) $result[$value] = [];
465 $result[$value][] = $key;
467 if(count($result) > $max){
469 foreach($array as $key => $value)
while(($value = preg_replace(
"/$word\$/",
'',$value,1,$count)) && $count){
470 if(!array_key_exists($value,$groups)) $groups[$value] = [];
471 $groups[$value][] = $key;
473 foreach($groups as $value => $keys)
474 if((count($keys) < $min) && preg_replace(
"/$word\$/",
'',$value,1,$count) && $count) unset($groups[$value]);
475 uksort($groups,
function($a,$b) use ($groups){
476 return ((count($groups[$a]) <=> count($groups[$b])) ?: (strlen($b) <=> strlen($a))) ?: ($a <=> $b);
479 $length = strlen($group_value = self::key($groups));
480 $count = count($group_keys = self::value($groups));
481 $groups = array_slice($groups,1,null,
true);
482 foreach($result as $value => $keys)
if((substr($value,0,$length) == $group_value) && preg_match(
"/^$word/",substr($value,$length))){
483 if(count($keys) == $count)
continue 2;
484 unset($result[$value]);
486 $result += [$group_value => $group_keys];
488 if($other && (count($result) < $max + $other)){
489 $others_count = $size = 0;
493 foreach($result as $value => $keys)
if(count($keys) == $size) $extra_others[$value] = $keys;
494 if(($others_count += array_sum(array_map(
'count',$extra_others))) <= $other) $others += $extra_others;
496 while($others_count < $other);
499 while((count($result) - count($others) > $max) && $groups);
501 $result = array_diff_key($result,$others);
502 $others = [null => call_user_func_array(
'array_merge',$others)];
505 return $others + self::sort($result,null,
true);
static key($array, $index=0)
Get the n-th key from an array.
static add(&$array, $key, $value=null)
Add a value to an array if the key does not already exist.
static deviation($array, &$average=null)
Calculate the mean average deviation of an array.
static group($array, $column)
Group records by a column.
static sort($array, $options=null, $key=false)
Sort an array.
static expand($array, $key_glue)
Brings a flattend array back to its multi-dimensional form.
static groupByLetter($array, $length=1, $case_sensitive=false)
Group a list of strings by its first letter(s).
static changeKey($array, $keys, $keep=true)
Change the keys of an array.
static merge(... $arrays)
Merge arrays while preserving (duplicate) key sequence.
static implode($array, $delimiter=null, $separator=null)
Enhanced implode.
static column($array, $column)
Return the values from a single column in the input array.
static resize($array, $length, $filler=null)
Set the length of an array.
static shuffle($array)
Shuffle an array.
static prefix($array, $prefix)
Add a prefix to all members of an array.
static explode($array, $delimiter=null, $separator=null)
Enhanced explode.
static iget($array, $key, $default=null)
Get a value from an array in a case insensitive way.
static value($array, $index=0, $default=null)
Get the n-th value from an array.
static select($array, $keys)
Return specific keys from an array.
static isearch($array, $value)
Searches the array for a given value and returns the first corresponding key if successful.
static combine($keys, $values, $filler=null)
Creates an array by using one array for keys and another for its values.
static average($array)
Calculate the average of an array.
static mergeKey($array, $key_name=null, $value_name=null)
Merge the key and value of an assoc.array into a record.
static splice($array, $offset, $length=0, $replace=null)
Splice with keys.
static prefixKey($array, $prefix)
Add a prefix to all keys of an array.
static exists($array, $key)
Checks if a key exists in an array.
static assoc($array)
Determine if an array is associative (that is, no ascending numerical key).
static ranges($array)
Retrieve numerical ranges from an array.
static flatten($array, $key_glue=null, $key_prefix=null)
Flattens a multi-dimensional array.
static groupByStart($array, $max=25, $other=0, $min=true, $word='\\W+\\w *')
Group a list of strings by its most common start.