array_reduce
Description
The array_reduce of Array for PHP iteratively reduce the array to a single value using a callback function.
Syntax
array_reduce( array $array, callable $callback, mixed $initial = null ): mixed
Parameters
array
The input array.
callback
callback( mixed $carry, mixed $item ): mixed
carry
Holds the return value of the previous iteration; in the case of the first iteration it instead holds the value of initial.
item
Holds the value of the current iteration.
initial
If the optional initial is available, it will be used at the beginning of the process, or as a final result in case the array is empty.
Return
Returns the resulting value.
Examples
1 · array callback
<? function myfunction($carry, $item) { $carry += $item; return $carry; } $array = array(1, 2, 3); $callback = "myfunction"; $return = array_reduce($array, $callback); print_r($return); ?>
6
2 · initial · Null
<? function myfunction($carry, $item) { $carry += $item; return $carry; } $array = array(1, 2, 3); $callback = "myfunction"; $initial = null; $return = array_reduce($array, $callback, $initial); print_r($return); ?>
6
3 · initial · Not Null
<? function myfunction($carry, $item) { $carry += $item; return $carry; } $array = array(1, 2, 3); $callback = "myfunction"; $initial = 4; $return = array_reduce($array, $callback, $initial); print_r($return); ?>
10
4 · array Empty · initial Null
<? function myfunction($carry, $item) { $carry += $item; return $carry; } $array = array(); $callback = "myfunction"; $initial = null; $return = array_reduce($array, $callback, $initial); var_export($return); ?>
NULL
5 · array Empty · initial Not Null
<? function myfunction($carry, $item) { $carry += $item; return $carry; } $array = array(); $callback = "myfunction"; $initial = "array is empty"; $return = array_reduce($array, $callback, $initial); print_r($return); ?>
array is empty
Links
Array
- array
- array_change_key_case
- array_chunk
- array_column
- array_combine
- array_count_values
- array_diff
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_fill
- array_fill_keys
- array_filter
- array_flip
- array_intersect
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_key_exists
- array_key_first
- array_key_last
- array_keys
- array_map
- array_merge
- array_merge_recursive
- array_multisort
- array_pad
- array_pop
- array_product
- array_push
- array_rand
- array_replace
- array_replace_recursive
- array_reverse
- array_search
- array_shift
- array_slice
- array_splice
- array_sum
- array_udiff
- array_udiff_assoc
- array_udiff_uassoc
- array_uintersect
- array_uintersect_assoc
- array_uintersect_uassoc
- array_unique
- array_unshift
- array_values
- array_walk
- array_walk_recursive
- arsort
- asort
- compact
- count
- current
- end
- extract
- in_array
- key
- key_exists
- krsort
- ksort
- list
- natcasesort
- natsort
- next
- pos
- prev
- range
- reset
- rsort
- shuffle
- sizeof
- sort
- uasort
- uksort
- usort