PHP Arrays
Like arrays in other languages, PHP arrays allow you to store multiple values in a single variable and operate on them as
a set. PHP offers an extensive array manipulation toolkit—over 60 functions—that lets you process arrays in almost any
way imaginable including reversing them, extracting subsets, comparing and sorting, recursively processing, and
searching them for specific values.
This document outlines some of the more useful functions in the PHP array toolkit, with explanations and usage
examples:
Function Explanation Example
sizeof($arr) This function returns the number of Code:
elements in an array. <?php
$data = array("red", "green",
Use this function to find out how many "blue");
elements an array contains; this
information is most commonly used to echo "Array has " . sizeof($data)
initialize a loop counter when . " elements";
processing the array. ?>
Output:
Array has 3 elements
array_values($arr) This function accepts a PHP array and Code:
returns a new array containing only its <?php
values (not its keys). Its counterpart is $data = array("hero" => "Holmes",
the array_keys() function. "villain" => "Moriarty");
print_r(array_values($data));
Use this function to retrieve all the ?>
values from an associative array.
Output:
Array
(
[0] => Holmes
[1] => Moriarty
)
array_keys($arr) This function accepts a PHP array and Code:
returns a new array containing only its <?php
keys (not its values). Its counterpart is $data = array("hero" => "Holmes",
the array_values() function. "villain" => "Moriarty");
print_r(array_keys($data));
Use this function to retrieve all the keys ?>
from an associative array.
Output:
Array
(
[0] => hero
[1] => villain
)
array_pop($arr) This function removes an element from Code:
the end of an array. <?php
$data = array("Donald", "Jim",
"Tom");
array_pop($data);
print_r($data);
?>
Output:
Array
(
[0] => Donald
[1] => Jim
)
Function Explanation Example
array_push($arr, $val) This function adds an element to the Code:
end of an array. <?php
$data = array("Donald", "Jim",
"Tom");
array_push($data, "Harry");
print_r($data);
?>
Output:
Array
(
[0] => Donald
[1] => Jim
[2] => Tom
[3] => Harry
)
array_shift($arr) This function removes an element from Code:
the beginning of an array. <?php
$data = array("Donald", "Jim",
"Tom");
array_shift($data);
print_r($data);
?>
Output:
Array
(
[0] => Jim
[1] => Tom
)
array_unshift($arr, This function adds an element to the Code:
$val) beginning of an array. <?php
$data = array("Donald", "Jim",
"Tom");
array_unshift($data, "Sarah");
print_r($data);
?>
Output:
Array
(
[0] => Sarah
[1] => Donald
[2] => Jim
[3] => Tom
)
Function Explanation Example
each($arr) This function is most often used to Code:
iteratively traverse an array. Each time <?php
each() is called, it returns the current $data = array("hero" => "Holmes",
key-value pair and moves the array "villain" => "Moriarty");
cursor forward one element. This while (list($key, $value) =
makes it most suitable for use in a loop. each($data)) {
echo "$key: $value \n";
}
?>
Output:
hero: Holmes
villain: Moriarty
sort($arr) This function sorts the elements of an Code:
array in ascending order. String values <?php
will be arranged in ascending $data = array("g", "t", "a",
alphabetical order. "s");
sort($data);
Note: Other sorting functions include print_r($data);
asort(), arsort(), ksort(), ?>
krsort() and rsort().
Output:
Array
(
[0] => a
[1] => g
[2] => s
[3] => t
)
array_flip($arr) The function exchanges the keys and Code:
values of a PHP associative array. <?php
$data = array("a" => "apple", "b"
Use this function if you have a tabular => "ball");
(rows and columns) structure in an print_r(array_flip($data));
array, and you want to interchange the ?>
rows and columns.
Output:
Array
(
[apple] => a
[ball] => b
)
Function Explanation Example
array_reverse($arr) The function reverses the order of Code:
elements in an array. <?php
$data = array(10, 20, 25, 60);
Use this function to re-order a sorted print_r(array_reverse($data));
list of values in reverse for easier ?>
processing—for example, when you're
trying to begin with the minimum or Output:
maximum of a set of ordered values. Array
(
[0] => 60
[1] => 25
[2] => 20
[3] => 10
)
array_merge($arr) This function merges two or more Code:
arrays to create a single composite <?php
array. Key collisions are resolved in $data1 = array("cat", "goat");
favor of the latest entry. $data2 = array("dog", "cow");
print_r(array_merge($data1,
Use this function when you need to $data2));
combine data from two or more arrays ?>
into a single structure—for example,
records from two different SQL queries. Output:
Array
(
[0] => cat
[1] => goat
[2] => dog
[3] => cow
)
array_rand($arr) This function selects one or more Code:
random elements from an array. <?php
$data = array("white", "black",
Use this function when you need to "red");
randomly select from a collection of echo "Today's color is " .
discrete values—for example, picking a $data[array_rand($data)];
random color from a list. ?>
Output:
Today's color is red
array_search($search, This function searches the values in an Code:
$arr) array for a match to the search term, <?php
and returns the corresponding key if $data = array("blue" =>
found. If more than one match exists, "#0000cc", "black" => "#000000",
the key of the first matching value is "green" => "#00ff00");
returned. echo "Found " .
array_search("#0000cc", $data);
Use this function to scan a set of index- ?>
value pairs for matches, and return the
matching index. Output:
Found blue
Function Explanation Example
array_slice($arr, This function is useful to extract a Code:
$offset, $length) subset of the elements of an array, as <?php
another array. Extracting begins from $data = array("vanilla",
array offset $offset and continues "strawberry", "mango",
until the array slice is $length "peaches");
elements long. print_r(array_slice($data, 1,
2));
Use this function to break a larger array ?>
into smaller ones—for example, when
segmenting an array by size Output:
("chunking") or type of data. Array
(
[0] => strawberry
[1] => mango
)
array_unique($data) This function strips an array of duplicate Code:
values. <?php
$data = array(1,1,4,6,7,4);
Use this function when you need to print_r(array_unique($data));
remove non-unique elements from an ?>
array—for example, when creating an
array to hold values for a table's Output:
primary key. Array
(
[0] => 1
[3] => 6
[4] => 7
[5] => 4
)
array_walk($arr, $func) This function "walks" through an array, Code:
applying a user-defined function to <?php
every element. It returns the changed function reduceBy10(&$val, $key)
array. {
$val -= $val * 0.1;
Use this function if you need to perform }
custom processing on every element of
an array—for example, reducing a $data = array(10,20,30,40);
number series by 10%. array_walk($data, 'reduceBy10');
print_r($data);
?>
Output:
Array
(
[0] => 9
[1] => 18
[2] => 27
[3] => 36
)