-
Notifications
You must be signed in to change notification settings - Fork 1
array.inc
- is_seq
- to_seq
- seq_filter
- assoc_map
- assoc_reduce
- assoc_filter
- assoc_exist
- assoc_for_all
- array_set
- array_unset
- array_hat
- array_shoe
- array_get
- array_get_or_else
- array_get_non_null
- array_get_non_empty
- array_filter_not
- array_flat
- array_zip
- array_head
- array_tail
- array_behead
- array_init
- array_last
- array_depeditate
- array_take
- array_take_right
- array_drop
- array_drop_right
- array_split
- array_slide
- array_exist
- array_for_all
- array_find
- array_partition
bool is_seq(array $array)
- Judge if
$arrayis the purely sequencial array. - Example:
var_dump(is_seq([1, 2, 3]) ); // => true
var_dump(is_seq([1 => 1, 2 => 2, 3 => 3])); // => falsearray to_seq(array $array)
- The alias of
array_values(). - Example:
var_dump(to_seq(['one' => 1, 'two' => 2, 'three' => 3])); // => [1, 2, 3]array seq_filter(array $array, Closure $closure)
- Return the array of the elements that
trueholds for$closure. - Notice: Indexes of the result array is purely sequencial.
- Example:
var_dump(seq_filter([1, 2, 3], function($e) { return $e % 2; })); // => [1, 3]array assoc_map(array $array, Closure $closure)
- Return the array which mapped by
$closure. - Notice:
$closureshould be defined asfunction($key, $value). - Example:
var_dump(assoc_map([1, 2, 3], function($k, $v) { return $k * $v; })); // => [0, 2, 6]mixed assoc_reduce(array $array, Closure $closure, mixed $initial)
- Return the value which reduced by
$closure. - Notice:
$closureshould be defined asfunction($carry, $key, $value). - Example:
var_dump(assoc_map([1, 2, 3], function($c, $k, $v) { return $c + $k * $v; }, 10)); // => 18array assoc_filter(array $array, Closure $closure)
- Return the array of elements that
trueholds for$closure. - Notice:
$closureshould befunction($key, $value). - Example:
var_dump(assoc_filter([1, 2, 3], function($k, $v) { return ($k * $v) % 3 === 0; })); // => [0, 6]bool assoc_exist(array $array, Closure $closure)
- Judge if
$arrayhas a element thattrueholds for$closure. - Notice:
$closureshould befunction($key, $value). - Example:
var_dump(assoc_exist([1, 1, 3], function($k, $v) { return $k === $v; })); // => true
var_dump(assoc_exist([1, 2, 3], function($k, $v) { return $k === $v; })); // => false- Judge if
$arrayhas only elements thattrueholds for$closure. - Notice:
$closureshould befunction($key, $value). - Example:
var_dump(assoc_for_all([1, 2, 3], function($k, $v) { return $k + 1 === $v; })); // => true
var_dump(assoc_for_all([1, 1, 3], function($k, $v) { return $k + 1 === $v; })); // => falsebool assoc_for_all(array $array, Closure $closure)
array rray_set(array $array, mixed $key, mixed $value)
- Return the array set
$valueat$key. - Example:
var_dump(array_set([], 'one', 1)); // => ['one' => 1]array array_unset(array $array, mixed $key)
- Return the array unset the value at
$key. - Example:
var_dump(array_unset(['one' => 1], 'one')); // => []array array_hat(array $array, mixed $hat)
- Return the array which added
$hatin the front. - Example:
var_dump(array_hat([1], 0)); // => [0, 1]array array_shoe(array $array, mixed $shoe)
- Return the array whicch added
$shoein the rear. - Example:
var_dump(array_shoe([1], 2)); // => [1, 2]mixed array_get(array &$array, mixed $key)
- Return the value at
$key. - Notice: If the value at
$keydoes not exist, returnnull. - Example:
$array = ['one' => 1];
var_dump(array_get($array, 'one')); // => 1
var_dump(array_get($array, 'two')); // => nullmixed array_get_or_else(array &$array, mixed $key, mixed $default)
- Return the value at
$key. - Notice: If the value at
$keydoes not exist, return$default. - Example:
$array = ['one' => 1];
var_dump(array_get_or_else($array, 'one', 2)); // => 1
var_dump(array_get_or_else($array, 'two', 2)); // => 2mixed array_get_non_null(array &$array, mixed $key, mixed $default)
- Return the value at
$key. - Notice: If the value at
$keyisnull, return$default. - Example:
$array = ['one' => 1, 'two' => null];
var_dump(array_get_non_null($array, 'one', 2)); // => 1
var_dump(array_get_non_null($array, 'two', 2)); // => 2
var_dump(array_get_non_null($array, 'key', 2)); // => 2mixed array_get_non_empty(array &$array, mixed $key, mixed $default)
- Return the value at
$key. - Notice: If the value at
$keyis empty, return$default. - Example:
$array = ['one' => 1, 'two' => 0];
var_dump(array_get_non_empty($array, 'one', 2)); // => 1
var_dump(array_get_non_empty($array, 'two', 2)); // => 2
var_dump(array_get_non_empty($array, 'key', 2)); // => 2array array_filter_not(array $array, Closure $closure = null)
- Return the filtered array whose
$closureis inverted. - Example:
var_dump(array_filter_not(['1', 1.1, 1], 'is_int')); // => ['1', 1.1]array array_flat(array $array)
- Return the array reduced by
array_merge(). - Example:
var_dump(array_flat([[1, 2], 3, 4, [[5], 6]])); // => [1, 2, 3, 4, [5], 6]array array_zip(array $first, array $second, ...)
- Return the array having elements of the combinations of each elements in
$first,$secondand more. - Notice: the length of result array is the minimum among lengths of
$first,$secondand more. - Example:
var_dump(array_zip([1, 2, 3], [4, 5, 6], [7, 8])); // => [[1, 4, 7], [2, 5, 8]]mixed array_head(array $array)
- Return the front element of
$array. - Notice: If
$arrayis empty, throwDomainException. - Example:
var_dump(array_head([1, 2, 3])); // => 1mixed array_tail(array $array)
- Return
$arrayexcluding the front element. - Notice: If
$arrayis empty, throwDomainException. - Example:
var_dump(array_tail([1, 2, 3])); // => [2, 3]array array_behead(array $array)
- Return the pair of array_head() and array_tail().
- Notice: If
$arrayis empty, throwDomainException. - Example:
var_dump(array_behead([1, 2, 3])); // => [1, [2, 3]]array array_init(array $array)
- Return
$arrayexcluding the rear element. - Notice: If
$arrayis empty, throwDomainException. - Example:
var_dump(array_init([1, 2, 3])); // => [1, 2]mixed array_last(array $array)
- Return the rear element of
$array. - Notice: If
$arrayis empty, throwDomainException. - Example:
var_dump(array_last([1, 2, 3])); // => 3array array_depeditate(array $array)
- Return the pair of array_init() and array_last().
- Notice: If
$arrayis empty, throwDomainException. - Example:
var_dump(array_depeditate([1, 2, 3])); // => [[1, 2], 3]array array_take(array $array, int $num)
- Return the array whose elements are token from the front of
$arrayby$num. - Notice:
$nummust be non-negative. - Example:
$array = [1, 2, 3];
var_dump(array_take($array, 0)); // => []
var_dump(array_take($array, 1)); // => [1]
var_dump(array_take($array, 2)); // => [1, 2]array array_take_right(array $array, int $num)
- Return the array whose elements are token from the rear of
$arrayby$num. - Notice:
$nummust be non-negative. - Example:
$array = [1, 2, 3];
var_dump(array_take_right($array, 0)); // => []
var_dump(array_take_right($array, 1)); // => [3]
var_dump(array_take_right($array, 2)); // => [2, 3]array_drop(array $array, int $num)
- Return the array whose elements are dropped from the front of
$arrayby$num. - Notice:
$nummust be non-negative. - Example:
$array = [1, 2, 3];
var_dump(array_drop($array, 0)); // => [1, 2, 3]
var_dump(array_drop($array, 1)); // => [2, 3]
var_dump(array_drop($array, 2)); // => [3]array_drop_right(array $array, int $num)
- Return the array whose elements are dropped from the rear of
$arrayby$num. - Notice:
$nummust be non-negative. - Example:
$array = [1, 2, 3];
var_dump(array_drop($array, 0)); // => [1, 2, 3]
var_dump(array_drop($array, 1)); // => [1, 2]
var_dump(array_drop($array, 2)); // => [1]array array_split(array $array, int $offset)
- Return the pair of arrays split at
$offset. - Example:
$array = [1, 2, 3];
var_dump(array_split($array, 0)); // => [[] , [1, 2, 3]]
var_dump(array_split($array, 1)); // => [[1] , [2, 3] ]
var_dump(array_split($array, -1)); // => [[1, 2], [3] ]array array_slide(array $array, int $size, int $step = 1)
- Return the arrays slided by
$stepwith the$sizewidth. - Example:
$array = [1, 2, 3];
var_dump(array_slide($array, 2 )); // => [[1, 2], [2, 3]]
var_dump(array_slide($array, 2, 2)); // => [[1, 2], [3]]bool array_exist(array $array, Closure $closure)
- Judge if
$arrayhas a element thattrueholds for$closure. - Example:
$array = [1, 2, 3];
var_dump(array_exist($array, function($e) { return $e % 2; })); // => true
var_dump(array_exist($array, function($e) { return $e % 4 === 0; })); // => falsebool array_for_all(array $array, Closure $closure)
- Judge if
$arrayhas only elements thattrueholds for$closure. - Example:
$array = [1, 2, 3];
var_dump(array_for_all($array, 'is_int' )); // => true
var_dump(array_for_all($array, function($e) { return $e % 2; })); // => falsemixed array_find(array $array, Closure $closure)
- Return the element of
$arraythattruefirst holds for$closure. - Notice: If there are no element that
trueholds, returnnull. - Example:
$array = [1, 2, 3];
var_dump(array_find($array, function($e) { return $e % 2; })); // => 1
var_dump(array_find($array, function($e) { return $e % 4 === 0; })); // => nullarray array_partition(array $array, Closure $closure)
- Return the pair of elements. "former" are what
trueholds for$closureand "latter" arefalse. - Example:
var_dump(array_partition([1, 2, 3], function($e) { return $e % 2; })); // => [[1, 3], [2]]Welcome any issure or pull request!
Please contact me by mail to tadatoshi.tokutake@gmail.com if you have some questions!