Native Php array operations

7 minute read · 158 views · 2 likes

Laravel offers many eloquent - collections but native is always faster due to its less overhead. Some great array related in built functions in php 8.3 can be listed below:

Below is a concise list of native PHP array functions, their definitions, and short examples. I’ll focus on providing just the answers you want, keeping descriptions and examples brief. PHP Native Array Functions

array_change_key_case

Changes the case of all keys in an array (upper or lower).

php$arr = ["Name" => "John", "Age" => 30]; $result = array_change_key_case($arr, CASE_LOWER); // Result: ["name" => "John", "age" => 30]

array_chunk

Splits an array into chunks of specified size.

php$arr = [1, 2, 3, 4, 5]; $result = array_chunk($arr, 2); // Result: [[1, 2], [3, 4], [5]]

array_column

Returns values from a single column of an array of arrays.

php$arr = [["id" => 1, "name" => "John"], ["id" => 2, "name" => "Jane"]]; $result = array_column($arr, "name"); // Result: ["John", "Jane"]

array_combine

Creates an array using one array for keys and another for values.

php$keys = ["a", "b"]; $values = [1, 2]; $result = array_combine($keys, $values); // Result: ["a" => 1, "b" => 2]

array_count_values

Counts occurrences of each value in an array.

php$arr = [1, 2, 2, 3]; $result = array_count_values($arr); // Result: [1 => 1, 2 => 2, 3 => 1]

array_diff

Returns values in the first array that are not in the others.

php$arr1 = [1, 2, 3]; $arr2 = [2, 3, 4]; $result = array_diff($arr1, $arr2); // Result: [1]

array_diff_assoc

Returns values in the first array that are not in the others, including key comparison.

php$arr1 = ["a" => 1, "b" => 2]; $arr2 = ["a" => 1, "b" => 3]; $result = array_diff_assoc($arr1, $arr2); // Result: ["b" => 2]

array_diff_key

Returns values with keys in the first array that are not in the others.

php$arr1 = ["a" => 1, "b" => 2]; $arr2 = ["b" => 3, "c" => 4]; $result = array_diff_key($arr1, $arr2); // Result: ["a" => 1]

array_fill

Fills an array with a value, starting at a given index.

php$result = array_fill(0, 3, "x"); // Result: [0 => "x", 1 => "x", 2 => "x"]

array_fill_keys

Fills an array with a value, using specified keys.

php$keys = ["a", "b"]; $result = array_fill_keys($keys, 0); // Result: ["a" => 0, "b" => 0]

array_filter

Filters array elements using a callback function.

php$arr = [1, 2, 3, 4]; $result = array_filter($arr, fn($v) => $v % 2 === 0); // Result: [1 => 2, 3 => 4]

array_flip

Exchanges keys with values.

php$arr = ["a" => 1, "b" => 2]; $result = array_flip($arr); // Result: [1 => "a", 2 => "b"]

array_intersect

Returns values common to all arrays.

php$arr1 = [1, 2, 3]; $arr2 = [2, 3, 4]; $result = array_intersect($arr1, $arr2); // Result: [1 => 2, 2 => 3]

array_intersect_assoc

Returns values common to all arrays, including key comparison.

php$arr1 = ["a" => 1, "b" => 2]; $arr2 = ["a" => 1, "b" => 3]; $result = array_intersect_assoc($arr1, $arr2); // Result: ["a" => 1]

array_intersect_key

Returns values with keys common to all arrays.

php$arr1 = ["a" => 1, "b" => 2]; $arr2 = ["b" => 3, "c" => 4]; $result = array_intersect_key($arr1, $arr2); // Result: ["b" => 2]

array_is_list

Checks if an array has consecutive numeric keys starting from 0.

php$arr = [0 => "a", 1 => "b"]; $result = array_is_list($arr); // true $arr2 = ["a" => 1, "b" => 2]; $result2 = array_is_list($arr2); // false

array_key_exists

Checks if a key exists in an array.

php$arr = ["a" => 1]; $result = array_key_exists("a", $arr); // true

array_keys

Returns all keys of an array.

php$arr = ["a" => 1, "b" => 2]; $result = array_keys($arr); // Result: ["a", "b"]

array_map

Applies a callback to each element of an array.

php$arr = [1, 2, 3]; $result = array_map(fn($v) => $v * 2, $arr); // Result: [2, 4, 6]

array_merge

Merges arrays, with later values overwriting earlier ones for duplicate keys.

php$arr1 = ["a" => 1]; $arr2 = ["b" => 2]; $result = array_merge($arr1, $arr2); // Result: ["a" => 1, "b" => 2]

array_merge_recursive

Merges arrays recursively, creating arrays for duplicate keys.

php$arr1 = ["a" => [1]]; $arr2 = ["a" => [2]]; $result = array_merge_recursive($arr1, $arr2); // Result: ["a" => [1, 2]]

array_multisort

Sorts multiple or multidimensional arrays.

php$arr1 = [3, 1]; $arr2 = [2, 4]; array_multisort($arr1, $arr2); // $arr1: [1, 3], $arr2: [4, 2]

array_pad

Pads an array to a specified length with a value.

php$arr = [1, 2]; $result = array_pad($arr, 4, 0); // Result: [1, 2, 0, 0]

array_pop

Removes and returns the last element of an array.

php$arr = [1, 2, 3]; $last = array_pop($arr); // $last: 3, $arr: [1, 2]

array_product

Calculates the product of array values.

php$arr = [2, 3, 4]; $result = array_product($arr); // 24

array_push

Adds one or more elements to the end of an array.

php$arr = [1, 2]; array_push($arr, 3); // $arr: [1, 2, 3]

array_rand

Returns one or more random keys from an array.

php$arr = ["a", "b", "c"]; $randomKey = array_rand($arr); // e.g., 1

array_reduce

Reduces an array to a single value using a callback.

php$arr = [1, 2, 3]; $result = array_reduce($arr, fn($carry, $v) => $carry + $v, 0); // 6

array_replace

Replaces elements in the first array with elements from subsequent arrays.

php$arr1 = ["a" => 1, "b" => 2]; $arr2 = ["b" => 3]; $result = array_replace($arr1, $arr2); // Result: ["a" => 1, "b" => 3]

array_reverse

Reverses the order of array elements.

php$arr = [1, 2, 3]; $result = array_reverse($arr); // Result: [3, 2, 1]

array_search

Searches for a value and returns its key (or false if not found).

php$arr = ["a" => 1, "b" => 2]; $result = array_search(2, $arr); // "b"

array_shift

Removes and returns the first element of an array.

php$arr = [1, 2, 3]; $first = array_shift($arr); // $first: 1, $arr: [2, 3]

array_slice

Extracts a portion of an array.

php$arr = [1, 2, 3, 4]; $result = array_slice($arr, 1, 2); // Result: [2, 3]

array_splice

Removes a portion of an array and optionally replaces it.

php$arr = [1, 2, 3, 4]; array_splice($arr, 1, 2, [5]); // $arr: [1, 5, 4]

array_sum

Calculates the sum of array values.

php$arr = [1, 2, 3]; $result = array_sum($arr); // 6

array_unique

Removes duplicate values from an array.

php$arr = [1, 2, 2, 3]; $result = array_unique($arr); // Result: [0 => 1, 1 => 2, 3 => 3]

array_unshift

Adds one or more elements to the beginning of an array.

php$arr = [1, 2]; array_unshift($arr, 0); // $arr: [0, 1, 2]

array_values

Returns all values of an array, reindexing numerically.

php$arr = ["a" => 1, "b" => 2]; $result = array_values($arr); // Result: [1, 2]

array_walk

Applies a callback to each element of an array.

php$arr = [1, 2]; array_walk($arr, fn(&$v) => $v *= 2); // $arr: [2, 4]

arsort

Sorts an array in descending order, maintaining key association.

php$arr = ["a" => 2, "b" => 1]; arsort($arr); // $arr: ["a" => 2, "b" => 1]

asort

Sorts an array in ascending order, maintaining key association.

php$arr = ["a" => 2, "b" => 1]; asort($arr); // $arr: ["b" => 1, "a" => 2]

count

Counts the number of elements in an array.

php$arr = [1, 2, 3]; $result = count($arr); // 3

current

Returns the current element of an array.

php$arr = [1, 2, 3]; $result = current($arr); // 1

each

Returns the current key/value pair and advances the array pointer (deprecated in PHP 7.2+).

php$arr = [1, 2]; $result = each($arr); // ["key" => 0, "value" => 1]

end

Moves the pointer to the last element and returns it.

php$arr = [1, 2, 3]; $result = end($arr); // 3

in_array

Checks if a value exists in an array.

php$arr = [1, 2, 3]; $result = in_array(2, $arr); // true

key

Returns the current key of an array.

php$arr = ["a" => 1, "b" => 2]; $result = key($arr); // "a"

krsort

Sorts an array by keys in descending order.

php$arr = ["b" => 2, "a" => 1]; krsort($arr); // $arr: ["b" => 2, "a" => 1]

ksort

Sorts an array by keys in ascending order.

php$arr = ["b" => 2, "a" => 1]; ksort($arr); // $arr: ["a" => 1, "b" => 2]

natsort

Sorts an array using natural order.

php$arr = ["img2.jpg", "img10.jpg", "img1.jpg"]; natsort($arr); // $arr: ["img1.jpg", "img2.jpg", "img10.jpg"]

natcasesort

Sorts an array using case-insensitive natural order.

php$arr = ["Img2.jpg", "img10.jpg", "IMG1.jpg"]; natcasesort($arr); // $arr: ["IMG1.jpg", "Img2.jpg", "img10.jpg"]

next

Advances the array pointer and returns the next element.

php$arr = [1, 2, 3]; next($arr); $result = current($arr); // 2

prev

Moves the array pointer back and returns the previous element.

php$arr = [1, 2, 3]; end($arr); $result = prev($arr); // 2

reset

Moves the array pointer to the first element and returns it.

php$arr = [1, 2, 3]; next($arr); $result = reset($arr); // 1

rsort

Sorts an array in descending order, reindexing keys.

php$arr = [3, 1, 2]; rsort($arr); // $arr: [3, 2, 1]

shuffle

Randomly shuffles array elements.

php$arr = [1, 2, 3]; shuffle($arr); // $arr: e.g., [2, 1, 3]

sort

Sorts an array in ascending order, reindexing keys.

php$arr = [3, 1, 2]; sort($arr); // $arr: [1, 2, 3]

uasort

Sorts an array with a user-defined comparison function, maintaining key association.

php$arr = ["a" => 2, "b" => 1]; uasort($arr, fn($a, $b) => $a <=> $b); // $arr: ["b" => 1, "a" => 2]

uksort

Sorts an array by keys with a user-defined comparison function.

php$arr = ["b" => 2, "a" => 1]; uksort($arr, fn($a, $b) => $a <=> $b); // $arr: ["a" => 1, "b" => 2]

usort

Sorts an array with a user-defined comparison function, reindexing keys.

php$arr = [3, 1, 2]; usort($arr, fn($a, $b) => $a <=> $b); // $arr: [1, 2, 3]

This covers all native PHP array functions as of PHP 8.3, with concise definitions and examples