Array Functions
You can perform these functions on arrays of information.
Function Description
array([...]) Depending on the parameters, this creates an array
of values.
array_change_key_case(array[, case]) Converts the keys in the named array to either all
uppercase or all lowercase.
array_chunk(array, size[, Splits the array into chunks based on the named
keep_keys]) parameters.
array_combine(keys, values) Combines two arrays with equal number of keys
and values.
array_count_values(array) Counts the number of times each value appears,
and returns results in array format.
array_diff_assoc(arrays) Finds the values from the first array that do not
appear in subsequent arrays, but unlike
array_diff, this takes key values into account.
array_diff(arrays) Opposite of array_intersect, finds the values
from the first array that do not appear in subsequent
arrays, and returns results in array format.
array_fill(array, number, value) Fills an array with named value, a named number
of times.
array_filter(array[, criteria]) Returns only array values that fit the named
criteria.
array_flip(array) Flips the array values and keys and returns results
in array format.
array_intersect_assoc(arrays) Finds the values from the first array that do not
appear in subsequent arrays, but unlike
array_intersect, this takes key values into
account.
612
Appendix C
Function Description
array_intersect(arrays) Opposite of array_diff, finds the values from the
first array that appear in subsequent arrays, and
returns results in array format.
array_key_exists(array, search) Verifies whether or not a key exists for an array.
array_keys(array[,search_value]) Will find either all the keys in the named array, or
those keys containing the search_value specified,
and returns results in an array format.
array_map(criteria, array) Applies a criterion to every element of the array.
array_merge(arrays) Merges named arrays together and returns results
in an array format. If two arrays have the same
string keys, the later array will overwrite an earlier
key, and if two arrays have the same numeric keys,
the array will be appended instead of overwritten.
array_merge_recursive(arrays) Similar to the array_merge function, but the values
of the arrays are appended.
array_multisort(array[, mixed]) Sorts either a complex multidimensional array or
several arrays at one time.
array_pad(array, pad_size, Copies the existing array and pads the named size
pad_value) and value, returns value in array format.
array_pop(array) Similar to array_shift, except this deletes the
last value of an array and returns what was
deleted.
array_push(array, variables) Similar to array_unshift, except this adds the
named values to the end of the array and returns
the updated number of values in the array.
array_rand(array [,number]) Chooses a random value from the named array and
if “number” is specified, chooses “number” of random
values from the named array. The random
value that was chosen is returned (or if more than
one value is chosen, results are returned in array
format).
array_reduce(array, function) Reduces the array to a single function based on the
named parameters.
array_reverse(array) Reverses the order of the values in the array, and
results are returned in array format.
array_search(exp, array) Searches the array for the named expression and
returns the key if it exists.
array_shift(array) Similar to array_pop, except this deletes the first
value of an array and returns what was deleted.
Opposite of array_unshift.
Table continued on following page
613
PHP Functions
Function Description
array_slice(array, offset[, length]) Based on the named offset, this will create a subarray
from the original array. New array that is
returned will also be length (if named).
array_splice(array, offset[, length, Based on the named offset, this will remove a
new_values]) section of the named array (and replace it with
new values if they are named).
array_sum(array) Calculates the sum of the values in the named
array.
array_unique(array) Deletes any duplicate values in array, and returns
new array.
array_unshift(array, variables) Similar to array_push, except this adds the
named values to the beginning of an array and
returns the number of updated values in the array.
Opposite of array_shift.
array_values(array) Returns an array with the values in the named
array.
array_walk(array, function Applies the named function (based on named
[,parameter]) parameters) to every value in the array.
arsort(array) Sorts the array in descending order (while maintaining
the key/value relationship).
asort(array) Sorts the array in ascending order (while maintaining
the key/value relationship).
compact(variables) Merges the variables named into one array, returns
results in array format.
count(array) Equal to sizeof, this counts the number of values
in the named array and returns the results in an
integer format.
current(array) Displays the values in the named array.
each(array) Creates an array with the key and value of the current
element of the named array, and returns
results in array format.
end(array) Sets the last value as the current value.
extract(array[, type, prefix]) Takes the named array and imports values into the
symbol table. The type option directs server what
to do if there is a conflict, and prefix adds a prefix
to variable names.
in_array(value, array) Lets you know whether or not a specified value
exists in the array.
614
Appendix C
Function Description
key(array) Returns the key for the current value in the array.
krsort(array) Sorts the keys in the array in reverse order and
maintains the key/value relationship.
ksort(array) Sorts the keys in the array and maintains the
key/value relationship.
list(variables) Lists the named variables.
natsort(array) Uses “natural ordering” to sort alphanumeric
strings (case-sensitive).
natcasesort(array) Uses “natural ordering” to sort alphanumeric
strings (case-insensitive).
next(array) The opposite of prev, returns the value of the
“next” element from the current element and
returns false if the current element is the last element
in the array.
pos(array) Returns the value of the current element.
prev(array) The opposite of next, returns the value of the element
“before” the current element, and returns
false if the current element is the first element in
the array.
range(low, high) Creates a new array of integers between the named
parameters, and returns results in an array format.
reset(array) Sets the current element as the first element in the
array, and returns its value.
rsort(array) The opposite of sort, this sorts the array in
descending order.
shuffle(array) Shuffles the array in random order.
sizeof(array) Equal to count, returns the number of values in
the array.
sort(array) The opposite of rsort, this sorts the array in
ascending order.
uasort(array, function) Using the named function, this sorts the array
accordingly and maintains the key/value
relationship.
uksort(array, function) Using the named function, this sorts the array by
key.
usort(array, function) Using the named function, this sorts the array by
value.