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:

  1. array() - Creates an array.

    • Example: $arr = array(1, 2, 3);
  2. array_change_key_case() - Changes the case of all keys in an array.

    • Example: array_change_key_case($arr, CASE_UPPER);
  3. array_chunk() - Splits an array into chunks.

    • Example: array_chunk([1, 2, 3, 4], 2);
  4. array_column() - Returns the values from a single column in a multidimensional array.

    • Example: array_column($arr, 'column_name');
  5. array_combine() - Creates an array by using one array for keys and another for values.

    • Example: array_combine($keys, $values);
  6. array_count_values() - Counts all the values in an array.

    • Example: array_count_values($arr);
  7. array_fill() - Fills an array with values.

    • Example: array_fill(0, 5, 'apple');
  8. array_fill_keys() - Fills an array with values, specifying keys.

    • Example: array_fill_keys(['a', 'b', 'c'], 1);
  9. array_filter() - Filters elements of an array using a callback function.

    • Example: array_filter($arr, 'is_even');
  10. array_flip() - Exchanges all keys with their associated values.

    • Example: array_flip($arr);

2. Searching and Sorting Arrays:

  1. array_key_exists() - Checks if a specified key exists in an array.

    • Example: array_key_exists('key', $arr);
  2. array_keys() - Returns all or a subset of keys from an array.

    • Example: array_keys($arr);
  3. array_merge() - Merges one or more arrays.

    • Example: array_merge($arr1, $arr2);
  4. array_merge_recursive() - Recursively merges two or more arrays.

    • Example: array_merge_recursive($arr1, $arr2);
  5. array_multisort() - Sorts multiple arrays or multidimensional arrays.

    • Example: array_multisort($arr1, SORT_ASC, $arr2, SORT_DESC);
  6. array_pad() - Pads an array to a specified length with a value.

    • Example: array_pad($arr, 5, 'apple');
  7. array_pop() - Pops the last element off the end of an array.

    • Example: array_pop($arr);
  8. array_push() - Pushes one or more elements onto the end of an array.

    • Example: array_push($arr, 'value');
  9. array_rand() - Picks one or more random keys from an array.

    • Example: array_rand($arr);
  10. array_reduce() - Iteratively reduces an array to a single value using a callback.

    • Example: array_reduce($arr, 'sum');
  11. array_replace() - Replaces elements from passed arrays into the first array.

    • Example: array_replace($arr1, $arr2);
  12. array_reverse() - Returns an array with elements in reverse order.

    • Example: array_reverse($arr);
  13. array_search() - Searches an array for a value and returns the corresponding key.

    • Example: array_search('needle', $arr);
  14. array_shift() - Shifts the first element off an array.

    • Example: array_shift($arr);
  15. array_slice() - Extracts a slice of the array.

    • Example: array_slice($arr, 2);
  16. array_splice() - Removes a portion of the array and replaces it with something else.

    • Example: array_splice($arr, 2, 2, ['new_value']);
  17. array_sum() - Returns the sum of values in an array.

    • Example: array_sum($arr);
  18. array_unique() - Removes duplicate values from an array.

    • Example: array_unique($arr);
  19. array_unshift() - Prepends one or more elements to the beginning of an array.

    • Example: array_unshift($arr, 'value');
  20. array_values() - Returns all the values of an array.

    • Example: array_values($arr);
  21. arsort() - Sorts an array in reverse order, preserving key associations.

    • Example: arsort($arr);
  22. asort() - Sorts an array and maintains index association.

    • Example: asort($arr);
  23. krsort() - Sorts an array by key in reverse order.

    • Example: krsort($arr);
  24. ksort() - Sorts an array by key in ascending order.

    • Example: ksort($arr);
  25. rsort() - Sorts an array in reverse order.

    • Example: rsort($arr);
  26. sort() - Sorts an array in ascending order.

    • Example: sort($arr);

3. Array Mapping and Manipulation:

  1. array_map() - Applies a callback to the elements of an array.

    • Example: array_map('strtoupper', $arr);
  2. array_walk() - Applies a user-defined function to each element of an array.

    • Example: array_walk($arr, 'my_function');
  3. array_walk_recursive() - Applies a user-defined function to each element of a multidimensional array.

    • Example: array_walk_recursive($arr, 'my_function');
  4. array_product() - Calculates the product of values in an array.

    • Example: array_product($arr);
  5. array_intersect() - Computes the intersection of arrays.

    • Example: array_intersect($arr1, $arr2);
  6. array_intersect_assoc() - Computes the intersection of arrays with additional index check.

    • Example: array_intersect_assoc($arr1, $arr2);
  7. array_intersect_key() - Computes the intersection of arrays using keys for comparison.

    • Example: array_intersect_key($arr1, $arr2);
  8. array_diff() - Computes the difference of arrays.

    • Example: array_diff($arr1, $arr2);
  9. array_diff_assoc() - Computes the difference of arrays with additional index check.

    • Example: array_diff_assoc($arr1, $arr2);
  10. array_diff_key() - Computes the difference of arrays using keys for comparison.

    • Example: array_diff_key($arr1, $arr2);

4. Other Array Functions:

  1. array_replace_recursive() - Recursively replaces elements from passed arrays into the first array.

    • Example: array_replace_recursive($arr1, $arr2);
  2. array_key_first() - Gets the first key of an array.

    • Example: array_key_first($arr);
  3. array_key_last() - Gets the last key of an array.

    • Example: array_key_last($arr);
  4. array_combine() - Creates an array by using one array for keys and another for values.

    • Example: array_combine($keys, $values);

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.