Skip to content
This repository was archived by the owner on Dec 9, 2025. It is now read-only.

array.inc

Tadatoshi Tokutake edited this page May 31, 2015 · 25 revisions

Index

Details

is_seq

bool is_seq(array $array)

  • Judge if $array is the purely sequencial array.
  • Example:
var_dump(is_seq([1, 2, 3])               ); // => true
var_dump(is_seq([1 => 1, 2 => 2, 3 => 3])); // => false

to_seq

array to_seq(array $array)

  • The alias of array_values().
  • Example:
var_dump(to_seq(['one' => 1, 'two' => 2, 'three' => 3])); // => [1, 2, 3]

seq_filter

array seq_filter(array $array, Closure $closure)

  • Return the array of the elements that true holds 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]

assoc_map

array assoc_map(array $array, Closure $closure)

  • Return the array which mapped by $closure.
  • Notice: $closure should be defined as function($key, $value).
  • Example:
var_dump(assoc_map([1, 2, 3], function($k, $v) { return $k * $v; })); // => [0, 2, 6]

assoc_reduce

mixed assoc_reduce(array $array, Closure $closure, mixed $initial)

  • Return the value which reduced by $closure.
  • Notice: $closure should be defined as function($carry, $key, $value).
  • Example:
var_dump(assoc_map([1, 2, 3], function($c, $k, $v) { return $c + $k * $v; }, 10)); // => 18

assoc_filter

array assoc_filter(array $array, Closure $closure)

  • Return the array of elements that true holds for $closure.
  • Notice: $closure should be function($key, $value).
  • Example:
var_dump(assoc_filter([1, 2, 3], function($k, $v) { return ($k * $v) % 3 === 0; })); // => [0, 6]

assoc_exist

bool assoc_exist(array $array, Closure $closure)

  • Judge if $array has a element that true holds for $closure.
  • Notice: $closure should be function($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

assoc_for_all

  • Judge if $array has only elements that true holds for $closure.
  • Notice: $closure should be function($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; })); // => false

bool assoc_for_all(array $array, Closure $closure)

array_set

array rray_set(array $array, mixed $key, mixed $value)

  • Return the array set $value at $key.
  • Example:
var_dump(array_set([], 'one', 1)); // => ['one' => 1]

array_unset

array array_unset(array $array, mixed $key)

  • Return the array unset the value at $key.
  • Example:
var_dump(array_unset(['one' => 1], 'one')); // => []

array_hat

array array_hat(array $array, mixed $hat)

  • Return the array which added $hat in the front.
  • Example:
var_dump(array_hat([1], 0)); // => [0, 1]

array_shoe

array array_shoe(array $array, mixed $shoe)

  • Return the array whicch added $shoe in the rear.
  • Example:
var_dump(array_shoe([1], 2)); // => [1, 2]

array_get

mixed array_get(array &$array, mixed $key)

  • Return the value at $key.
  • Notice: If the value at $key does not exist, return null.
  • Example:
$array = ['one' => 1];
var_dump(array_get($array, 'one')); // => 1
var_dump(array_get($array, 'two')); // => null

array_get_or_else

mixed array_get_or_else(array &$array, mixed $key, mixed $default)

  • Return the value at $key.
  • Notice: If the value at $key does 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)); // => 2

array_get_non_null

mixed array_get_non_null(array &$array, mixed $key, mixed $default)

  • Return the value at $key.
  • Notice: If the value at $key is null, 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)); // => 2

array_get_non_empty

mixed array_get_non_empty(array &$array, mixed $key, mixed $default)

  • Return the value at $key.
  • Notice: If the value at $key is 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)); // => 2

array_filter_not

array array_filter_not(array $array, Closure $closure = null)

  • Return the filtered array whose $closure is inverted.
  • Example:
var_dump(array_filter_not(['1', 1.1, 1], 'is_int')); // => ['1', 1.1]

array_flat

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_zip

array array_zip(array $first, array $second, ...)

  • Return the array having elements of the combinations of each elements in $first, $second and more.
  • Notice: the length of result array is the minimum among lengths of $first, $second and more.
  • Example:
var_dump(array_zip([1, 2, 3], [4, 5, 6], [7, 8])); // => [[1, 4, 7], [2, 5, 8]]

array_head

mixed array_head(array $array)

  • Return the front element of $array.
  • Notice: If $array is empty, throw DomainException.
  • Example:
var_dump(array_head([1, 2, 3])); // => 1

array_tail

mixed array_tail(array $array)

  • Return $array excluding the front element.
  • Notice: If $array is empty, throw DomainException.
  • Example:
var_dump(array_tail([1, 2, 3])); // => [2, 3]

array_behead

array array_behead(array $array)

var_dump(array_behead([1, 2, 3])); // => [1, [2, 3]]

array_init

array array_init(array $array)

  • Return $array excluding the rear element.
  • Notice: If $array is empty, throw DomainException.
  • Example:
var_dump(array_init([1, 2, 3])); // => [1, 2]

array_last

mixed array_last(array $array)

  • Return the rear element of $array.
  • Notice: If $array is empty, throw DomainException.
  • Example:
var_dump(array_last([1, 2, 3])); // => 3

array_depeditate

array array_depeditate(array $array)

var_dump(array_depeditate([1, 2, 3])); // => [[1, 2], 3]

array_take

array array_take(array $array, int $num)

  • Return the array whose elements are token from the front of $array by $num.
  • Notice: $num must 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_take_right

array array_take_right(array $array, int $num)

  • Return the array whose elements are token from the rear of $array by $num.
  • Notice: $num must 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_drop(array $array, int $num)

  • Return the array whose elements are dropped from the front of $array by $num.
  • Notice: $num must 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_drop_right(array $array, int $num)

  • Return the array whose elements are dropped from the rear of $array by $num.
  • Notice: $num must 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_split

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_slide

array array_slide(array $array, int $size, int $step = 1)

  • Return the arrays slided by $step with the $size width.
  • 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]]

array_exist

bool array_exist(array $array, Closure $closure)

  • Judge if $array has a element that true holds 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; })); // => false

array_for_all

bool array_for_all(array $array, Closure $closure)

  • Judge if $array has only elements that true holds 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; })); // => false

array_find

mixed array_find(array $array, Closure $closure)

  • Return the element of $array that true first holds for $closure.
  • Notice: If there are no element that true holds, return null.
  • 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; })); // => null

array_partition

array array_partition(array $array, Closure $closure)

  • Return the pair of elements. "former" are what true holds for $closure and "latter" are false.
  • Example:
var_dump(array_partition([1, 2, 3], function($e) { return $e % 2; })); // => [[1, 3], [2]]

Clone this wiki locally