PHP - Array functions

  »   PHP Introduction  »   PHP Tutorial - Array functions

PHP has a very comprehensive list of built-in functions to work with arrays. Please take into consideration that working with big and complicated arrays takes a lot of memory usage. You can recycle that memory just by destroying the array if you do not need it anymore.

Here are some simple examples of how you can use some of the array functions in the list below.

php<?php

$file_list = array(
	"index" 	=> "home_page.php",
	"article" 	=> "article_page.php",
	"about" 	=> "about_page.php",
	"contact" 	=> "contact_page.php"
);

/*
 * Loop through the array and print all the values
 */
if(is_array($file_list)){
	foreach($file_list as $file){ // iterate files
		echo $file . "<br />";
	}
}


/*
 * If about key exists inside our array, get his value
 */
$key = "about";
if(array_key_exists($key, $file_list) ){
	echo "This is the file that contains the about template: " .$file_list[$key];
}


/*
 * Join two arrays into one array and then count all his elements and print them.
 */
$image_list = array(
	"favicon" => "favicon.png",
	"header" => "header.png",
	"footer" => "footer.png"
);
$all_files = array_merge($file_list, $image_list);

echo "The new created array has ". count($all_files) ." items.<br />";

echo "<pre>";
print_r($all_files);  // print_r is a very usefull function used for debugging
echo "</pre>";
?>

Here is a very extensive list with some other useful functions to play around with arrays in PHP.

Array functionDescription
is_arrayFinds whether a variable is an array
explodeSplit a string by a delimiter and returns an array
implodeJoin array elements with a string
splitSplit string into array by regular expression (deprecated)
preg_splitSplit string by a regular expression
unsetDestroys the specified variables.
array_change_key_caseChanges the case of all keys in an array
array_chunkSplit an array into chunks
array_columnReturn the values from a single column in the input array
array_combineCreates an array by using one array for keys and another for its values
array_count_valuesCounts all the values of an array
array_diff_assocComputes the difference of arrays with additional index check
array_diff_keyComputes the difference of arrays using keys for comparison
array_diff_uassocComputes the difference of arrays with additional index check which is performed by a user supplied callback function
array_diff_ukeyComputes the difference of arrays using a callback function on the keys for comparison
array_diffComputes the difference of arrays
array_fill_keysFill an array with values, specifying keys
array_fillFill an array with values
array_filterFilters elements of an array using a callback function
array_flipExchanges all keys with their associated values in an array
array_intersect_assocComputes the intersection of arrays with additional index check
array_intersect_keyComputes the intersection of arrays using keys for comparison
array_intersect_uassocComputes the intersection of arrays with additional index check, compares indexes by a callback function
array_intersect_ukeyComputes the intersection of arrays using a callback function on the keys for comparison
array_intersectComputes the intersection of arrays
array_key_existsChecks if the given key or index exists in the array
array_key_firstGets the first key of an array
array_key_lastGets the last key of an array
array_keysReturn all the keys or a subset of the keys of an array
array_mapApplies the callback to the elements of the given arrays
array_merge_recursiveMerge one or more arrays recursively
array_mergeMerge one or more arrays
array_multisortSort multiple or multi-dimensional arrays
array_padPad array to the specified length with a value
array_popPop the element off the end of array
array_productCalculate the product of values in an array
array_pushPush one or more elements onto the end of array
array_randPick one or more random keys out of an array
array_reduceIteratively reduce the array to a single value using a callback function
array_replace_recursiveReplaces elements from passed arrays into the first array recursively
array_replaceReplaces elements from passed arrays into the first array
array_reverseReturn an array with elements in reverse order
array_searchSearches the array for a given value and returns the first corresponding key if successful
array_shiftShift an element off the beginning of array
array_sliceExtract a slice of the array
array_spliceRemove a portion of the array and replace it with something else
array_sumCalculate the sum of values in an array
array_udiff_assocComputes the difference of arrays with additional index check, compares data by a callback function
array_udiff_uassocComputes the difference of arrays with additional index check, compares data and indexes by a callback function
array_udiffComputes the difference of arrays by using a callback function for data comparison
array_uintersect_assocComputes the intersection of arrays with additional index check, compares data by a callback function
array_uintersect_uassocComputes the intersection of arrays with additional index check, compares data and indexes by separate callback functions
array_uintersectComputes the intersection of arrays, compares data by a callback function
array_uniqueRemoves duplicate values from an array
array_unshiftPrepend one or more elements to the beginning of an array
array_valuesReturn all the values of an array
array_walk_recursiveApply a user function recursively to every member of an array
array_walkApply a user supplied function to every member of an array
arrayCreate an array
arsortSort an array in reverse order and maintain index association
asortSort an array and maintain index association
compactCreate array containing variables and their values
countCount all elements in an array, or something in an object
currentReturn the current element in an array
eachReturn the current key and value pair from an array and advance the array cursor
endSet the internal pointer of an array to its last element
extractImport variables into the current symbol table from an array
in_arrayChecks if a value exists in an array
key_existsAlias of array_key_exists
keyFetch a key from an array
krsortSort an array by key in reverse order
ksortSort an array by key
listAssign variables as if they were an array
natcasesortSort an array using a case insensitive "natural order" algorithm
natsortSort an array using a "natural order" algorithm
nextAdvance the internal pointer of an array
posAlias of current
prevRewind the internal array pointer
rangeCreate an array containing a range of elements
resetSet the internal pointer of an array to its first element
rsortSort an array in reverse order
shuffleShuffle an array
sizeofAlias of count
sortSort an array
uasortSort an array with a user-defined comparison function and maintain index association
uksortSort an array by keys using a user-defined comparison function
usortSort an array by values using a user-defined comparison function