PHP Array functions List
PHP provides a comprehensive set of array functions for manipulating and working with arrays. Here's a list of important array functions:
1. Creating and Manipulating Arrays:
array()
- Creates an array.- Example:
$arr = array(1, 2, 3);
- Example:
array_change_key_case()
- Changes the case of all keys in an array.- Example:
array_change_key_case($arr, CASE_UPPER);
- Example:
array_chunk()
- Splits an array into chunks.- Example:
array_chunk([1, 2, 3, 4], 2);
- Example:
array_column()
- Returns the values from a single column in a multidimensional array.- Example:
array_column($arr, 'column_name');
- Example:
array_combine()
- Creates an array by using one array for keys and another for values.- Example:
array_combine($keys, $values);
- Example:
array_count_values()
- Counts all the values in an array.- Example:
array_count_values($arr);
- Example:
array_fill()
- Fills an array with values.- Example:
array_fill(0, 5, 'apple');
- Example:
array_fill_keys()
- Fills an array with values, specifying keys.- Example:
array_fill_keys(['a', 'b', 'c'], 1);
- Example:
array_filter()
- Filters elements of an array using a callback function.- Example:
array_filter($arr, 'is_even');
- Example:
array_flip()
- Exchanges all keys with their associated values.- Example:
array_flip($arr);
- Example:
2. Searching and Sorting Arrays:
array_key_exists()
- Checks if a specified key exists in an array.- Example:
array_key_exists('key', $arr);
- Example:
array_keys()
- Returns all or a subset of keys from an array.- Example:
array_keys($arr);
- Example:
array_merge()
- Merges one or more arrays.- Example:
array_merge($arr1, $arr2);
- Example:
array_merge_recursive()
- Recursively merges two or more arrays.- Example:
array_merge_recursive($arr1, $arr2);
- Example:
array_multisort()
- Sorts multiple arrays or multidimensional arrays.- Example:
array_multisort($arr1, SORT_ASC, $arr2, SORT_DESC);
- Example:
array_pad()
- Pads an array to a specified length with a value.- Example:
array_pad($arr, 5, 'apple');
- Example:
array_pop()
- Pops the last element off the end of an array.- Example:
array_pop($arr);
- Example:
array_push()
- Pushes one or more elements onto the end of an array.- Example:
array_push($arr, 'value');
- Example:
array_rand()
- Picks one or more random keys from an array.- Example:
array_rand($arr);
- Example:
array_reduce()
- Iteratively reduces an array to a single value using a callback.- Example:
array_reduce($arr, 'sum');
- Example:
array_replace()
- Replaces elements from passed arrays into the first array.- Example:
array_replace($arr1, $arr2);
- Example:
array_reverse()
- Returns an array with elements in reverse order.- Example:
array_reverse($arr);
- Example:
array_search()
- Searches an array for a value and returns the corresponding key.- Example:
array_search('needle', $arr);
- Example:
array_shift()
- Shifts the first element off an array.- Example:
array_shift($arr);
- Example:
array_slice()
- Extracts a slice of the array.- Example:
array_slice($arr, 2);
- Example:
array_splice()
- Removes a portion of the array and replaces it with something else.- Example:
array_splice($arr, 2, 2, ['new_value']);
- Example:
array_sum()
- Returns the sum of values in an array.- Example:
array_sum($arr);
- Example:
array_unique()
- Removes duplicate values from an array.- Example:
array_unique($arr);
- Example:
array_unshift()
- Prepends one or more elements to the beginning of an array.- Example:
array_unshift($arr, 'value');
- Example:
array_values()
- Returns all the values of an array.- Example:
array_values($arr);
- Example:
arsort()
- Sorts an array in reverse order, preserving key associations.- Example:
arsort($arr);
- Example:
asort()
- Sorts an array and maintains index association.- Example:
asort($arr);
- Example:
krsort()
- Sorts an array by key in reverse order.- Example:
krsort($arr);
- Example:
ksort()
- Sorts an array by key in ascending order.- Example:
ksort($arr);
- Example:
rsort()
- Sorts an array in reverse order.- Example:
rsort($arr);
- Example:
sort()
- Sorts an array in ascending order.- Example:
sort($arr);
- Example:
3. Array Mapping and Manipulation:
array_map()
- Applies a callback to the elements of an array.- Example:
array_map('strtoupper', $arr);
- Example:
array_walk()
- Applies a user-defined function to each element of an array.- Example:
array_walk($arr, 'my_function');
- Example:
array_walk_recursive()
- Applies a user-defined function to each element of a multidimensional array.- Example:
array_walk_recursive($arr, 'my_function');
- Example:
array_product()
- Calculates the product of values in an array.- Example:
array_product($arr);
- Example:
array_intersect()
- Computes the intersection of arrays.- Example:
array_intersect($arr1, $arr2);
- Example:
array_intersect_assoc()
- Computes the intersection of arrays with additional index check.- Example:
array_intersect_assoc($arr1, $arr2);
- Example:
array_intersect_key()
- Computes the intersection of arrays using keys for comparison.- Example:
array_intersect_key($arr1, $arr2);
- Example:
array_diff()
- Computes the difference of arrays.- Example:
array_diff($arr1, $arr2);
- Example:
array_diff_assoc()
- Computes the difference of arrays with additional index check.- Example:
array_diff_assoc($arr1, $arr2);
- Example:
array_diff_key()
- Computes the difference of arrays using keys for comparison.- Example:
array_diff_key($arr1, $arr2);
- Example:
4. Other Array Functions:
array_replace_recursive()
- Recursively replaces elements from passed arrays into the first array.- Example:
array_replace_recursive($arr1, $arr2);
- Example:
array_key_first()
- Gets the first key of an array.- Example:
array_key_first($arr);
- Example:
array_key_last()
- Gets the last key of an array.- Example:
array_key_last($arr);
- Example:
array_combine()
- Creates an array by using one array for keys and another for values.- Example:
array_combine($keys, $values);
- Example:
These array functions in PHP allow for versatile and powerful manipulation of arrays, helping to handle a wide variety of data-processing tasks. Whether you need to sort, filter, search, or modify arrays, PHP provides a vast toolkit to meet your needs.