Skip to content

Latest commit

 

History

History
352 lines (238 loc) · 7.88 KB

01-arrays.md

File metadata and controls

352 lines (238 loc) · 7.88 KB

Array functions

Associative arrays

  • A general solution for the problem of wanting to know the keys in the callback, and/or retain the key association in the returned array.
array_map_assoc(callable $callback, array $array, array ...$arrays): array
  • Array slice function that works with associative arrays (keys)
array_slice_assoc(array $array, array $keys): array
  • Inverse associative version of array_slice_assoc() function
array_slice_assoc_inverse(array $array, array $keys): array
  • Remove a portion of the associative array and replace it with something else
array_splice_assoc(array $array, mixed $offset, mixed $length, mixed $replacement): array

Recursive arrays

  • Searches the recursive array for a given value and returns the first corresponding array value if successful
array_search_recursive(array $needle, array $haystack): array
  • Get all values from specific key in a multidimensional array
array_values_recursive(string $key, array $arr): mixed
  • Shift an element off the beginning of multidimensional array
array_shift_recursive(array $array): array
  • Get duplicate and unique values from an array
array_unique_recursive(array $array, mixed $key): array
  • Find all the keys from a multidimensional array while keeping the array structure
array_keys_recursive($array, $max_depth = INF, $depth = 0, $array_keys = []): array
  • Recursive in_array function
in_array_recursive(mixed $needle, array $haystack, bool $strict): bool
  • Applies the callback to the elements of the given recursive arrays
array_map_recursive(callable $callback, array $array): array
  • Calculate the sum of values in an multidimensional array
array_sum_recursive(array $array): int|float
  • array_column implementation that works on multidimensional arrays (not just 2-dimensional)
array_column_recursive(array $haystack, mixed $needle): array
  • Push a multidimensional numeric array into another
array_push_recursive(array $array1, array $array2): array

array_values() complements

  • Get the first value of array
array_value_first(object|array $array): mixed
  • Checks if all of given array's elements have a non-falsy value
array_value_empty($array): bool
  • Count the total number of times a specific value appears in array
array_value_count(mixed $match, array $array): int
  • Returns the last value from an array
array_value_last(array $array): mixed

array_column() complements

  • Group values by the same index_key in arrays one
array_column_group_key(array $array, int|string $column_key, int|string $index_key): array

array_search() complements

  • Get all key=>value associations of an array with the given search-value
array_search_with_keys(mixed $needle, mixed $haystack, bool $strict = false): array

array_push() complements

  • Add elements to an array before a specific index or key
array_push_before(array $src, array $in, int|string $pos): array
  • Add elements to an array after a specific index or key
array_push_after(array $src, array $in, int|string $pos): array
  • A function which mimics push() from perl, perl lets you push an array to an array
array_push_array(array &$array): array

array_combine() complements

  • Preserve duplicate keys when combining arrays
array_combine_identical_keys(array $keys, array $values): array
  • Creates an array by using one array for keys and another for its values if they are not of same size
array_combine_different_size(array $a, array $b): array

array_change_key_case() complements

  • Changes the case of all keys in an Unicode array
array_change_key_case_unicode(array $arr, int $c = CASE_LOWER): array
  • Returns an array with all values lowercased or uppercased.
array_change_value_case(array $array, int $case = CASE_LOWER): array

Array contains

  • Check if an array contains all values in a set.

This function works as expected with nested arrays or an array with objects.

array_contains_all(array $array, array $subset, boolean $strict = false): bool
  • Check if an array contains all values in a set with index check.

This function works as expected with nested arrays or an array with objects.

array_contains_all_assoc(array $array, array $subset, boolean $strict = false): bool
  • Check if an array contains any value in a set.

This function works as expected with nested arrays or an array with objects.

array_contains_any(array $array, array $subset, boolean $strict = false): bool
  • Check if an array contains any value in a set with index check.

This function works as expected with nested arrays or an array with objects.

array_contains_any_assoc(array $array, array $subset, boolean $strict = false): bool

Others functions for arrays

  • Checks if multiple keys exist in an array
array_many_keys_exists(array $array, array|string $keys): bool
  • array_splice with preserve keys
array_splice_preserve_keys(array $array, int $offset, ?int $length = null): array
  • Flatten a nested associative array, concatenating the keys.
array_flatten(string $glue, array $array): array

Example

$values = array_flatten('.', [
    'animal' => [
        'mammel' => [
            'ape',
            'bear'
        ],
        'reptile' => 'chameleon'
    ],
    'colors' => [
        'red' => 60,
        'green' => 100,
        'blue' => 0
    ]
]);

Will become

[
    'animal.mammel' => [
        'ape',
        'bear'
    ],
    'animal.reptile' => 'chameleon',
    'colors.red' => 60,
    'colors.green' => 100,
    'colors.blue' => 0
]
  • This function simplifies removal of a value from an array, when the index is not known.
array_delete(array $array, string $value, bool $strict = true): array
  • Delete the given key or index in the array
array_key_delete(array $array, mixed $key, bool $strict = true): array
  • Flip an array and group the elements by value
array_group(array $array): array
  • Add an value to array
array_add(array $array, mixed $value, bool $strict = true): array|false
  • If two arrays' values are exactly the same (regardless of keys and order)
array_identical_values(array $array1, array $array2): bool
  • List all the files and folders you want to exclude in a project directory
array_preg_diff(string $needle, string $pattern): string
  • Convert array to UTF-8
array_encode_utf8(array $array, string $source_encoding): array
  • Join an array, using the 'and' parameter as glue the last two items.
array_join_pretty(string $glue, string $and, array $array);

Example

echo "A task to " . array_join_pretty(", ", " and ", $chores) . " has been created.", PHP_EOL;
echo array_join_pretty(", ", " or ", $names) . " may pick up this task.", PHP_EOL;
  • Return an array with only the specified keys.
array_only(array $array, array $keys): array
  • Return an array without the specified keys.
array_without(array $array, array $keys): array
  • Find an element of an array using a callback function. Returns the value or FALSE if no element was found.
array_find(array $array, callable $callback, int $flag = 0): mixed

Flag determining what arguments are sent to callback:

  • ARRAY_FILTER_USE_KEY - pass key as the only argument to callback instead of the value
  • ARRAY_FILTER_USE_BOTH - pass both value and key as arguments to callback instead of the value
  • Default is 0 which will pass value as the only argument to callback instead.
  • Find a key of an array using a callback function. Returns the key or FALSE if no element was found.
array_find_key(array $array, callable $callback, int $flag = 0): mixed