PHP Array Functions

PHP provides a comprehensive set of functions for working with arrays. These functions enable you to manipulate, sort, and analyze arrays in various ways. Below is a categorized list of useful PHP array functions, along with examples to illustrate their usage.Array Creation and Initialization

Array Creation and Initialization

array(): Creates an array.

php
$fruits = array("Apple", "Banana", "Cherry");

range($start, $end): Creates an array containing a range of elements.

php
$numbers = range(1, 5); // [1, 2, 3, 4, 5]


Array Access and Manipulation


 array_push(&$array, $value1, $value2, ...): Adds one or more elements to the end of an array.

php
$numbers = array(1, 2);
array_push($numbers, 3, 4); // $numbers is now [1, 2, 3, 4]


array_pop(&$array): Removes the last element from an array.

php
$last = array_pop($numbers); // $numbers is now [1, 2, 3], $last is 4


array_shift(&$array):
Removes the first element from an array.

php
$first = array_shift($numbers); // $numbers is now [2, 3], $first is 1

array_unshift(&$array, $value1, $value2, ...): Adds one or more elements to the beginning of an array.

php
array_unshift($numbers, 1); // $numbers is now [1, 2, 3]

array_splice(&$array, $offset, $length = null, $replacement = array()): Removes or replaces elements from an array.

php
    array_splice($numbers, 1, 2, array(4, 5)); // $numbers is now [1, 4, 5]


Array Sorting


sort(&$array, $sort_flags = SORT_REGULAR): Sorts an array in ascending order.

php
$numbers = array(3, 1, 4, 1, 5);
sort($numbers); // $numbers is now [1, 1, 3, 4, 5]


rsort(&$array, $sort_flags = SORT_REGULAR): Sorts an array in descending order.

php
rsort($numbers); // $numbers is now [5, 4, 3, 1, 1]


asort(&$array, $sort_flags = SORT_REGULAR): Sorts an array and maintains index association.

php
$assoc = array("a" => 3, "b" => 1, "c" => 2);
asort($assoc); // $assoc is now ["b" => 1, "c" => 2, "a" => 3]

ksort(&$array, $sort_flags = SORT_REGULAR): Sorts an array by key.

php
ksort($assoc); // $assoc is now ["a" => 3, "b" => 1, "c" => 2]


Array Search and Filtering


 in_array($needle, $haystack, $strict = false): Checks if a value exists in an array.

php
$exists = in_array(2, $numbers); // true

array_search($needle, $haystack, $strict = false): Searches for a value in an array and returns its key.

php
$key = array_search(2, $numbers); // $key is 1

array_filter($array, $callback = null, $flag = 0): Filters elements of an array using a callback function.

php
$evens = array_filter($numbers, function($value) {
    return $value % 2 == 0;
}); // $evens is [4]

array_map($callback, $array1, $array2, ...): Applies a callback function to the elements of one or more arrays.

php
    $squares = array_map(function($value) {
        return $value * $value;
    }, $numbers); // $squares is [25, 16, 9, 1, 1]


Array Reduction and Aggregation

array_reduce($array, $callback, $initial = null): Iteratively reduces the array to a single value using a callback function.

php
$sum = array_reduce($numbers, function($carry, $item) {
    return $carry + $item;
}, 0); // $sum is 15

array_sum($array): Computes the sum of the values in an array.

php
$total = array_sum($numbers); // $total is 15

array_product($array): Computes the product of the values in an array.

php
$product = array_product($numbers); // $product is 120


Array Information

array_keys($array, $search_value = null, $strict = false): Returns all the keys of an array.

php
$keys = array_keys($assoc); // $keys is ["a", "b", "c"]

array_values($array): Returns all the values of an array.

php
$values = array_values($assoc); // $values is [3, 1, 2]

count($array, $mode = COUNT_NORMAL): Counts all elements in an array.

php
$count = count($numbers); // $count is 5


Array Combining and Splitting

 array_merge($array1, $array2, ...): Merges one or more arrays into one.

php
$combined = array_merge($numbers, $more_numbers); // Combines two arrays

array_slice($array, $offset, $length = null, $preserve_keys = false): Extracts a slice of the array.

php
    $slice = array_slice($numbers, 1, 3); // $slice is [2, 3, 4]


Array Unique

array_unique($array, $sort_flags = SORT_STRING): Removes duplicate values from an array.

php
$unique = array_unique(array(1, 2, 2, 3, 4, 4)); // $unique is [1, 2, 3, 4]


This overview covers a range of functions that are crucial for effective array manipulation in PHP. For a complete reference, you can always consult the PHP official documentation.




  • To Share this Link, Choose your plateform


Our Other Tutorials