There are over 70 array functions available in PHP, but listed below are 40 of the most useful (and understandable) functions.
Function | Description |
---|---|
array() | Create an array |
array_change_key_case() | Changes all keys in an array |
array_chunk() | Split an array into chunks |
array_combine() | Creates an array by using one array for keys and another for its values |
array_count_values() | Counts all the values of an array |
array_flip() | Exchanges all keys with their associated values in an array |
array_keys() | Return all the keys or a subset of the keys of an array |
array_merge() | Merge one or more arrays |
array_multisort() | Sort multiple or multi-dimensional arrays |
array_pad() | Pad array to the specified length with a value |
array_product() | Calculate the product of values in an array |
array_rand() | Pick one or more random entries out of an array |
array_reverse() | Return an array with elements in reverse order |
array_search() | Searches the array for a given value and returns the corresponding key if successful |
array_slice() | Extract a slice of the array |
array_splice() | Remove a portion of the array and replace it with something else |
array_sum() | Calculate the sum of values in an array |
array_unique() | Removes duplicate values from an array |
arsort() | Sort an array in reverse order and maintain index association |
asort() | Sort an array and maintain index association |
compact() | Create array containing variables and their values |
count() | Count all elements in an array, or properties in an object |
current() | Return the current element in an array |
each() | Return the current key and value pair from an array and advance the array cursor |
end() | Set the internal pointer of an array to its last element |
in_array() | Checks if a value exists in an array |
is_array() | Checks if a variable is an array |
key() | Fetch a key from an array |
krsort() | Sort an array by key in reverse order |
ksort() | Sort an array by key |
list() | Assign variables as if they were an array |
natcasesort() | Sort an array using a case insensitive "natural order" algorithm |
natsort() | Sort an array using a "natural order" algorithm |
next() | Advance the internal array pointer of an array |
prev() | Rewind the internal array pointer |
range() | Create an array containing a range of elements |
reset() | Set the internal pointer of an array to its first element |
rsort() | Sort an array in reverse order |
shuffle() | Shuffle an array |
sizeof() | Alias of count |
sort() | Sort an array |
Each function accepts different arguments in order to produce its result. They are all documented in the official PHP manual, but we will run through a few common example here.
The sort() function accepts a single argument (the array name) and re-arranges each value in that array from lowest to highest, or in alphabetical order. If the array is an associative array, the existing keys will be removed and numerical keys will be assigned in their place. If the array is already numerical, the number keys will simply be re-arranged.
The count() function will return how many elements (values) there are in an array.
The range() function creates an array containing a range of numbers between the two numbers specified as arguments. If a third numerical argument is presented, it is used as the unit of incrementation. The default third argument is 1.
Let's take a look at these three functions in action.
<?php
$fruit = array("orange", "pineapple", "peach", "apple", "pear", "cherry");
sort($fruit);
print_r($fruit);
echo count($fruit);
$range_array = range(0, 50, 5);
print_r($range_array);
?>
And the results are:
Array
(
[0] => apple
[1] => cherry
[2] => orange
[3] => peach
[4] => pear
[5] => pineapple
)
6
Array
(
[0] => 0
[1] => 5
[2] => 10
[3] => 15
[4] => 20
[5] => 25
[6] => 30
[7] => 35
[8] => 40
[9] => 45
[10] => 50
)
Assignment: Use shuffle() to mix up the fruit array, then use array_search() to determine which key that the value "pineapple" ends up under, and echo "pineapple" from the result of the search.