Please enable JavaScript.
Coggle requires JavaScript to display documents.
PHP Array Functions, Chunk, slice, Combine Array, Calculation - Coggle…
PHP Array Functions
Array Modifier
- array_map(callable $callback, array $array, ... $arrays)
- array_change_key_case($array, int $case = CASE_LOWER)
- array_flip($array):
- Exchanges all keys with associated values
- array_reverse($array, $preserve_key = false);
- Reverse an array with elements in reverse order
- array_walk(&$array, callable $callback, mixed $arg): bool
- Apply a user supplied function to every member of an array
- array_walk_recursive(array|object &$array, callable $callback, mixed $arg = null): bool
- shuffle(array &$array): true
- Shuffle an array
-
Key Handler
array_key_exists(string|int $key, array $array): bool
-
-
array_keys($array): return all keys of an array
array_keys($array, $filter_value, bool $strict = false): filter keys by array value
-
Sort
By Values
- asort(array &$array, int $flags = SORT_REGULAR): true
- Sort an array in ascending order and maintain index association
- Sort by values
- sort(array &$array, int $flags = SORT_REGULAR): true
- Sort an array in ascending order
- sort by values
- rsort(array &$array, int $flags = SORT_REGULAR): true
- Sort an array in descending order
- sort by values
- arsort(&$array, int $flags = SORT_REGULAR): true
- Sort an array in descending order and maintain index association
- Sort by values
By Keys
- krsort(array &$array, int $flags = SORT_REGULAR): true
- Sort an array by key in descending order
- ksort(array &$array, int $flags = SORT_REGULAR): true
- Sort an array by key in ascending order
Natural Sort
- natsort(array &$array): true
- This function implements a sort algorithm that orders alphanumeric strings in the way a human being would
- while maintaining key/value associations
- natcasesort(array &$array): true
- Sort an array using a case insensitive "natural order" algorithm
user-defined sort
- uasort(array &$array, callable $callback): true
- Sort an array with a user-defined comparison function and maintain index association
- sort by values
- uksort(array &$array, callable $callback): true
- Sort an array by keys using a user-defined comparison function
- sort by keys
- usort(array &$array, callable $callback): true
- sort by values
- without maining index association
Multi-dimensional
- array_multisort( &$array1, $array1_sort_order, $array1_sort_flags, ...$rest ): bool
- sort multiple or multi-dimensional arrays
add or remove or replace
Add
array_fill($start_index, $count, $mixed): like create a new array with $value
array_pad($array, $length, mixed $value): array
-
-
Remove
array_pop(array &$array):
- pops and returns the value of the last element of array, shortening the array by one element.
-
replace
array_replace($array, ...$replacements): array
- replaces the values of array with values having the same keys in each of the following arrays.
- If the key exists in the second array, and not the first, it will be created in the first array
array_replace_recursive($array, ...$replacements): array
- It will recurse into arrays and apply the same process to the inner value
remove and replace
array_splice(&$array, int $offset, ?int $length, $replacement = [])
- Remove a portion of an array and replace it with something else ($replacement)
Create and Extract
- compact(array|string $var_name, array|string ...$var_names): array
- Create array containing variables and their values
- array(mixed ...$values): array
- extract(array &$array, int $flags = EXTR_OVERWRITE, string $prefix = ""): int
- Import variables from an array into the current symbol table.
- list(mixed $var, mixed ...$vars = ?): array
- list() is used to assign a list of variables in one operation.
- Strings can not be unpacked and list() expressions can not be completely empty.
$info = array('coffee', 'brown', 'caffeine');
list($drink, $color, $power) = $info;
- range(string|int|float $start, string|int|float $end, int|float $step = 1): array
- Create an array containing a range of elements.
Retrieval and Filter
Counter
-
count($array, $mode = COUNT_NORMAL): count all elements in an array or in a Countable oject
-
Filter
array_filter($array, callable $callback = null, int $mode = 0): array
retrieval
array_rand($array, $num = 1);
- pick one or more keys out of an arrray
array_search($needle, $haystack, $strict = false);
- Searches the array for a given value and return the first corresponding key if successful
array_unique($array, $flags = SORT_STRING)
- removes dublicate values from an array
array_values($array): array
- return the values of an array
check values
in_array($needle, $haystack, $strict = false)
- If the third parameter is set to true then the in_array() function will also check the types of the needle in the haystack
array_key_exists($key, $array)
array_search($needle, $haystack, $strict = false);
- Searches the array for a given value and return the first corresponding key if successful
array_is_list($array): bool
list must have integer key, key in order, and consecutive
compare array
get diff
array_diff
compare key
array_diff_ukey(array $array, array ...$arrays, callable $key_compare_func): array
array_diff_key($array, ... $arrays); array
compare value
array_diff_uassoc($array, ...$arrays, callable $key_compare_func): array
array_diff_assoc($array, ... $arrays): array
array_diff($array, ...$array)
unintersect
array_uintersect_assoc(array $array, array ...$arrays, callable $value_compare_func): array
array_unintersect_uassoc(array $array1, array ...$arrays, callable $value_compare_func, callable $key_compare_func ): array
array_uintersect(array $array, array ...$arrays, callable $value_compare_func): array
Get the same
Intersection
array_intersect_ukey($array, ...$arrays, $key_compare_func)
array_intersect_key($array, ...$arrays)
array_intersect_uassoc($array, ...$arrays, callable $key_compare_func)
array_intersect_assoc($array, ...$array)
array_udiff
array_udiff_assoc(array $array, array ...$arrays, callable $value_compare_func): array
array_udiff_uassoc( array $array, array ...$arrays, callable $value_compare_func, callable $key_compare_func ): array
array_udiff(array $array, array ...$arrays, callable $value_compare_func): array
collection
array_column(array $array, $column_key, $index_key = null)
Chunk, slice
array_chunk(array $array, int $length, bool $preserve_keys = false): array
array_slice(array $array, int $offset, ?int $length = null, $preserve_keys = false)
- Returns a sequence of elements from the array
-
array_splice(&$array, int $offset, ?int $length, $replacement = [])
- Remove a portion of an array and replace it with something else ($replacement)
Combine Array
array_combine(array $key, array $values);
-
-
-
Calculation
array_product($array): int|float
- array_product(1, 2, 3, 4) = 1 2 3 * 4 = 24
array_reduce($array, $callback, mixed $initial = null);
- Iteratively reduce the array to a single value using a callback function
-
array_sum($array);
- calculate the sum of values in an array