Array Functions - Zend PHP 5 Certification Study Guide (2014)

Zend PHP 5 Certification Study Guide (2014)

Appendix A: Array Functions

Function

Summary

array_change_key_case

Allows you to duplicate an array and change all keys to upper or lowercase

array_chunk

Allows you to split an array into chunks of a specified size.

array_column

Added in PHP 5.5. Return the values from a single “column” in a multi-dimensional array.

array_count_values

Returns a count of each distinct value in the array

array_diff_assoc

Returns key-value pairs from two different arrays, that exist only in the second array.

array_diff_key

Similar to array_diff_assoc(), except that it only takes keys into account.

array_diff_uassoc

Identical to array_diff_assoc(), except that a user-defined callback function is used for the comparisons.

array_diff_ukey

Identical to array_diff_key(), except that, like array_diff_uassoc(), a user-defined callback is used for the comparison.

array_diff

Returns values that exist in a second array, but not in the first. Keys are ignored.

array_fill

Create a new array with any number of elements, using the same value for each.

array_filter

Allows you to filter an array, removing elements you don’t want, based on a callback function.

array_flip

Returns a new array with the array key-value pairs flipped around, making the values the keys and vice-versa.

array_intersect_assoc

Similar to array_diff_assoc(), except that it returns those key-value pairs that are matched in both arrays.

array_intersect_key

As array_diff_assoc(), except that it returns an array of key-value pairs, where there keys are matched in both arrays.

array_intersect_uassoc

Identical to array_intersect_assoc(), however it uses a callback to determine matches for values.

array_intersect_ukey

Identical to array_intersect_key(), however, like array_intersect_uassoc(), it uses a callback to determine the matches.

array_intersect

Returns values that exist only in both arrays passed as arguments.

array_key_exists

Checks if the given key exists in an array

array_keys

Returns an array containing the keys of the given array

array_map

Given two or more arrays, array_map() will pass the nth value of each array as an argument to a callback.

array_merge_recursive

Merges two or more arrays recursively, this means that the nth child array is merged with its sibling.

array_merge

Merge one or more single-dimension array.

array_multisort

Mimics SQL ORDER BY functionality using multiple arrays for the “columns”.

array_pad

Using a specified value, pad an array so that it meets the required length.

array_product

Calculate the product of an array (that is, multiple each number by the next, by the next, and so forth).

array_rand

Retrieve a random value from the array.

array_reduce

Iteratively reduce the array to a single value using a callback function.

array_replace

Replaces elements from passed arrays into the first array

array_replace_recursive

Replaces elements from passed arrays into the first array recursively

array_reverse

Reverse the elements in an array and return it.

array_search

Searches an array for a given value and returns the corresponding key if found, otherwise, false.

array_slice

Return a portion of an array

array_splice

Replace a portion of an array with another array.

array_sum

Return the sum of all the values in the array.

array_udiff_assoc

Return the difference between two arrays using a callback function. Differences are based on key-value pairs.

array_udiff_uassoc

Similar to array_udiff_assoc() except a different callback is used for keys and for values.

array_udiff

Return the difference between two arrays using a callback. Differences are based on values only.

array_uintersect_assoc

Return the common elements of two arrays using a callback to compare the values. Differences are based on key-value pairs.

array_uintersect_uassoc

Identical to array_uintersect_assoc() however it uses a callback for both the key and the value comparisons.

array_uintersect

Identical to array_uintersect_assoc() except that it only does comparisons based on the values.

array_unique

Returns an array with all duplicate values removed.

array_values

Returns all values of an array as an enumerative array.

array_walk

Apply a callback function to each key-value pair in an array.

compact

Will create an array using the variable names passed in, from the current scope.

count/sizeof

Returns the number of elements in an array (not recursive).

current

Returns the current element in an array, leaving the internal pointer alone.

each

Returns the current element of an array, and moves the internal pointer to the next. Convenient shortcut for current() and next().

end

Move an arrays internal pointer to the last element

extract

Create variables for each value of the array in the current scope using the key as the variable name. Only keys whose names are valid variable identifiers are extracted (i.e. non-numeric).

in_array

Checks if a value exists in an array (not recursive).

key

Returns the key for the current array position

next

Advance the internal array pointer of an array by one, and return that value

pos

Alias for current().

prev

Rewind the internal array pointer of an array by one, and return that value

range

Create a new array containing a range of values, i.e. 0-9 or A-Z.