Comprehensive Guide to PHP Array Functions

This tutorial presents a thorough overview of PHP array functions, explaining key‑value operations, creation and splitting utilities, sorting techniques, and set operations such as differences and intersections, while providing clear code examples for each function.

php Courses
php Courses
php Courses
Comprehensive Guide to PHP Array Functions

This article provides a detailed overview of common PHP array functions, covering basic key‑value operations, array creation and splitting, sorting, and set operations like differences and intersections.

1. Basic key and value functions

Retrieve all keys: array_keys() Retrieve all values: array_values() Swap keys and values: array_flip() Check if a value exists: in_array() Search for a value and get its key: array_search() Check if a key exists: isset() or array_key_exists() Count elements: count() (alias sizeof())

Change key case: array_change_key_case() (CASE_UPPER / CASE_LOWER)

Count value occurrences: array_count_values() Get first/last key: array_key_first(), array_key_last() Pop last element: array_pop() Push or unshift elements: array_push(), array_unshift() Reverse array: array_reverse() Sum or product of values: array_sum(), array_product() Remove duplicate values: array_unique() Shuffle array: shuffle() Get random keys:

array_rand()
$arr_keys = array_keys($array);
$arr_values = array_values($arr);
$arr2 = array_flip($arr);
$bool = in_array('hello', $arr);
$bool = array_search('hello', $arr);
$bool = array_key_exists('a', $arr);
$n = count($arr); // same as sizeof($arr)
$lowarr = array_change_key_case($arr, CASE_LOWER);
$arr_count = array_count_values($arr);
$key = array_key_first($arr);
$last = array_pop($array);
$new_array = array_push($array, $value1, $value2, ...);
$new_array = array_unshift($array, $value1, $value2, ...);
$reverse = array_reverse($arr);
$sum = array_sum($array);
$product = array_product($array);
$bool = shuffle($arr);
$randKeys = array_rand($arr, 1);

2. Creation and splitting functions

Split array into chunks: array_chunk($array, $size, $preserve_keys) Combine two arrays into key/value pairs: array_combine($keys, $values) Fill array with keys and a common value: array_fill_keys($keys, $value) Fill array with a repeated value: array_fill($start_index, $num, $value) Merge arrays: array_merge() (non‑recursive) and array_merge_recursive() Pad array to a certain length: array_pad($array, $size, $value) Extract a slice: array_slice($array, $offset, $length, $preserve_keys) Splice array: array_splice($array, $offset, $length, $replacement) Compact variables into an array: compact($var1, $var2, ...) Extract variables from an array: extract($array) Assign array values to variables: list($var1, $var2, ...) Create a range of values:

range($start, $end, $step)
$myarr = array_chunk($arr, 2);
$arr_1 = ['A','B','C'];
$arr_2 = ['a','b','c'];
$arr_3 = array_combine($arr_1, $arr_2);
$keys = array('foo', 5, 10, 'bar');
$a = array_fill_keys($keys, 'banana');
$arr = array_fill(0, 10, 'myname');
$a = array_merge($arr_1, $arr_2);
$arr = array_pad($arr, 12, 'x');
$slice = array_slice($arr, 2, 5);
array_splice($arr, 3, 2, ['new1','new2']);
list($drink, , $power) = array('coffee','brown','caffeine');
$range = range(0,8,2);

3. Sorting functions

The primary sorting function is sort(), which can be combined with flags such as SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_NATURAL, and SORT_FLAG_CASE. Variants include rsort() (reverse), ksort() / krsort() (key sorting), asort() / arsort() (value sorting while preserving keys), and user‑defined sorts like usort(), uksort(), uasort(). Natural sorting functions natsort() and natcasesort() are also covered.

/* bool sort(array &$array [, int $sort_flags = SORT_REGULAR]) */

4. Array set operations

Difference: array_diff(), array_udiff(), array_diff_key(), array_diff_ukey(), array_diff_assoc(), array_diff_uassoc(), array_udiff_assoc(), array_udiff_uassoc() Intersection: array_intersect(), array_uintersect(), array_intersect_key(), array_intersect_ukey(), array_intersect_assoc(), array_intersect_uassoc(), array_uintersect_assoc(),

array_uintersect_uassoc()
$diff = array_diff($array1, $array2);
$intersect = array_intersect($array1, $array2);

This guide serves as a quick reference for developers working with PHP arrays, offering concise explanations and ready‑to‑use code snippets for each function.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPTutorial
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.