You are on page 1of 11

PHP: Array Functions - Manual

1 of 11

http://php.net/manual/en/ref.array.php

Downloads
Documentation
Get Involved
Help
Getting Started
Introduction
A simple tutorial
Language Reference
Basic syntax
Types
Variables
Constants
Expressions
Operators
Control Structures
Functions
Classes and Objects
Namespaces
Errors
Exceptions
Generators
References Explained
Predefined Variables
Predefined Exceptions
Predefined Interfaces and Classes
Context options and parameters
Supported Protocols and Wrappers
Security
Introduction
General considerations
Installed as CGI binary
Installed as an Apache module
Filesystem Security
Database Security
Error Reporting
Using Register Globals
User Submitted Data
Magic Quotes
Hiding PHP
Keeping Current
Features
HTTP authentication with PHP
Cookies

11/4/2015 8:19 AM

PHP: Array Functions - Manual

2 of 11

http://php.net/manual/en/ref.array.php

Sessions
Dealing with XForms
Handling file uploads
Using remote files
Connection handling
Persistent Database Connections
Safe Mode
Command line usage
Garbage Collection
DTrace Dynamic Tracing

Function Reference
Affecting PHP's Behaviour
Audio Formats Manipulation
Authentication Services
Command Line Specific Extensions
Compression and Archive Extensions
Credit Card Processing
Cryptography Extensions
Database Extensions
Date and Time Related Extensions
File System Related Extensions
Human Language and Character Encoding Support
Image Processing and Generation
Mail Related Extensions
Mathematical Extensions
Non-Text MIME Output
Process Control Extensions
Other Basic Extensions
Other Services
Search Engine Extensions
Server Specific Extensions
Session Extensions
Text Processing
Variable and Type Related Extensions
Web Services
Windows Only Extensions
XML Manipulation
Keyboard Shortcuts
?
This help
j
Next menu item
k
Previous menu item
gp
Previous man page
gn
Next man page
G

11/4/2015 8:19 AM

PHP: Array Functions - Manual

3 of 11

gg
gh
gs
/

http://php.net/manual/en/ref.array.php

Scroll to bottom
Scroll to top
Goto homepage
Goto search
(current page)
Focus search box

array_change_key_case
Sorting Arrays

PHP Manual
Function Reference
Variable and Type Related Extensions
Arrays

Change language:

Edit Report a Bug

Array Functions
See Also

See also is_array(), explode(), implode(), split(), preg_split(), and unset().

Table of Contents

array_change_key_case Changes the case of all keys in an array


array_chunk Split an array into chunks
array_column Return the values from a single column in the input array
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_diff_assoc Computes the difference of arrays with additional index check
array_diff_key Computes the difference of arrays using keys for comparison
array_diff_uassoc Computes the difference of arrays with additional index check which is
performed by a user supplied callback function
array_diff_ukey Computes the difference of arrays using a callback function on the keys for
comparison
array_diff Computes the difference of arrays
array_fill_keys Fill an array with values, specifying keys
array_fill Fill an array with values
array_filter Filters elements of an array using a callback function
array_flip Exchanges all keys with their associated values in an array
array_intersect_assoc Computes the intersection of arrays with additional index check
array_intersect_key Computes the intersection of arrays using keys for comparison

11/4/2015 8:19 AM

PHP: Array Functions - Manual

4 of 11

http://php.net/manual/en/ref.array.php

array_intersect_uassoc Computes the intersection of arrays with additional index check, compares
indexes by a callback function
array_intersect_ukey Computes the intersection of arrays using a callback function on the keys for
comparison
array_intersect Computes the intersection of arrays
array_key_exists Checks if the given key or index exists in the array
array_keys Return all the keys or a subset of the keys of an array
array_map Applies the callback to the elements of the given arrays
array_merge_recursive Merge two or more arrays recursively
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_pop Pop the element off the end of array
array_product Calculate the product of values in an array
array_push Push one or more elements onto the end of array
array_rand Pick one or more random entries out of an array
array_reduce Iteratively reduce the array to a single value using a callback function
array_replace_recursive Replaces elements from passed arrays into the first array recursively
array_replace Replaces elements from passed arrays into the first 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_shift Shift an element off the beginning of array
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_udiff_assoc Computes the difference of arrays with additional index check, compares data by
a callback function
array_udiff_uassoc Computes the difference of arrays with additional index check, compares data
and indexes by a callback function
array_udiff Computes the difference of arrays by using a callback function for data comparison
array_uintersect_assoc Computes the intersection of arrays with additional index check, compares
data by a callback function
array_uintersect_uassoc Computes the intersection of arrays with additional index check, compares
data and indexes by separate callback functions
array_uintersect Computes the intersection of arrays, compares data by a callback function
array_unique Removes duplicate values from an array
array_unshift Prepend one or more elements to the beginning of an array
array_values Return all the values of an array
array_walk_recursive Apply a user function recursively to every member of an array
array_walk Apply a user supplied function to every member of an array
array Create 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 something 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
extract Import variables into the current symbol table from an array
in_array Checks if a value exists in an array
key_exists Alias of array_key_exists

11/4/2015 8:19 AM

PHP: Array Functions - Manual

5 of 11

http://php.net/manual/en/ref.array.php

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
pos Alias of current
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
uasort Sort an array with a user-defined comparison function and maintain index association
uksort Sort an array by keys using a user-defined comparison function
usort Sort an array by values using a user-defined comparison function
add a note

User Contributed Notes 11 notes


up
down
14
kolkabes at googlemail dot com
3 years ago

Short function for making a recursive array copy while cloning objects on the way.
<?php
function arrayCopy( array $array ) {
$result = array();
foreach( $array as $key => $val ) {
if( is_array( $val ) ) {
$result[$key] = arrayCopy( $val );
} elseif ( is_object( $val ) ) {
$result[$key] = clone $val;
} else {
$result[$key] = $val;
}
}
return $result;
}
?>

up
down
11
renatonascto at gmail dot com
7 years ago

Big arrays use a lot of memory possibly resulting in memory limit errors. You can reduce

11/4/2015 8:19 AM

PHP: Array Functions - Manual

6 of 11

http://php.net/manual/en/ref.array.php

memory usage on your script by destroying them as soon as youre done with them. I was
able to get over a few megabytes of memory by simply destroying some variables I didnt
use anymore.
You can view the memory usage/gain by using the funcion memory_get_usage(). Hope this
helps!

up
down
2
dave at davidhbrown dot us
4 years ago

While PHP has well over three-score array functions, array_rotate is strangely missing
as of PHP 5.3. Searching online offered several solutions, but the ones I found have
defects such as inefficiently looping through the array or ignoring keys.
The following array_rotate() function uses array_merge and array_shift to reliably
rotate an array forwards or backwards, preserving keys. If you know you can trust your
$array to be an array and $shift to be between 0 and the length of your array, you can
skip the function definition and use just the return expression in your code.
<?php
function array_rotate($array, $shift) {
if(!is_array($array) || !is_numeric($shift)) {
if(!is_array($array)) error_log(__FUNCTION__.' expects first argument to be
array; '.gettype($array).' received.');
if(!is_numeric($shift)) error_log(__FUNCTION__.' expects second argument to be
numeric; '.gettype($shift)." `$shift` received.");
return $array;
}
$shift %= count($array); //we won't try to shift more than one array length
if($shift < 0) $shift += count($array);//handle negative shifts as positive
return array_merge(array_slice($array, $shift, NULL, true), array_slice($array, 0,
$shift, true));
}
?>
A few simple tests:
<?php
$array=array("foo"=>1,"bar"=>2,"baz"=>3,4,5);
print_r(array_rotate($array,
print_r(array_rotate($array,
print_r(array_rotate($array,
print_r(array_rotate($array,
print_r(array_rotate($array,
?>

up
down
0
callmeanaguma at gmail dot com
2 years ago

2));
-2));
count($array)));
"4"));
-9));

If you need to flattern two-dismensional array with single values assoc subarrays, you
could use this function:

11/4/2015 8:19 AM

PHP: Array Functions - Manual

7 of 11

http://php.net/manual/en/ref.array.php

<?php
function arrayFlatten($array) {
$flattern = array();
foreach ($array as $key => $value){
$new_key = array_keys($value);
$flattern[] = $value[$new_key[0]];
}
return $flattern;
}
?>

up
down
0
seva dot lapsha at gmail dot com
6 years ago

Arrays are good, but inapplicable when dealing with huge amounts of data.
I'm working on rewriting some array functions to operate with plain Iterators - map,
reduce, walk, flip et cetera are already there.
In addition I'm going to implement simulation of comprehensions (generators) in PHP
(http://en.wikipedia.org/wiki/List_comprehension).
See the source code, examples and documentation at http://code.google.com/p/phpiterator-utils/

up
down
-2
mo dot longman at gmail dot com
8 years ago

up
down
-5
ob at babcom dot biz
9 years ago

11/4/2015 8:19 AM

PHP: Array Functions - Manual

8 of 11

http://php.net/manual/en/ref.array.php

up
down
-7
cyberchrist at futura dot net
8 years ago

up
down
-9
rune at zedeler dot dk
8 years ago

11/4/2015 8:19 AM

PHP: Array Functions - Manual

9 of 11

http://php.net/manual/en/ref.array.php

up
down
-9
oliverSPAMMENOT at e-geek dot com dot au
5 years ago

11/4/2015 8:19 AM

PHP: Array Functions - Manual

10 of 11

http://php.net/manual/en/ref.array.php

up
down
-14
Jck_true (leave out the &#39;_&#39; at gmail dot com)
7 years ago

11/4/2015 8:19 AM

PHP: Array Functions - Manual

11 of 11

http://php.net/manual/en/ref.array.php

add a note

Arrays
Introduction
Installing/Configuring
Predefined Constants
Sorting Arrays
Array Functions

Copyright 2001-2015 The PHP Group


My PHP.net
Contact
Other PHP.net sites
Mirror sites
Privacy policy

11/4/2015 8:19 AM

You might also like