Array functions
Arrays
array
Create an array
Description
array array
Returns an array of the parameters. The parameters can be given
an index with the => operator.
array is a language construct used to represent
literal arrays, and not a regular function.
The following example demonstrates how to create a
two-dimensional array, how to specify keys for associative
arrays, and how to skip-and-continue numeric indices in normal
arrays.
array example
$fruits = array(
"fruits" => array("a"=>"orange","b"=>"banana","c"=>"apple"),
"numbers" => array(1, 2, 3, 4, 5, 6),
"holes" => array("first", 5 => "second", "third")
);
See also:
list.
array_walk
Apply a user function to every member of an array.
Description
int array_walk
array arr
string func
Applies the function named by func to each
element of arr. The elements are passed as
the first argument of func; if
func requires more than one argument, a
warning will be generated each time
array_walk calls
func. These warnings may be suppressed by
prepending the '@' sign to the array_walk
call, or by using error_reporting.
func will actually be working with the
elements of arr, so any changes made to
those elements will be made in the array itself.
array_walk example
$fruits = array("d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple");
function test_alter( $item1 ) {
$item1 = 'bogus';
}
function test_print( $item2 ) {
echo "$item2<br>\n";
}
array_walk( $fruits, 'test_print' );
array_walk( $fruits, 'test_alter' );
array_walk( $fruits, 'test_print' );
See also each and list.
arsort
Sort an array in reverse order and maintain index association
Description
void arsort
array array
This function sorts an array such that array indices maintain their
correlation with the array elements they are associated with. This is
used mainly when sorting associative arrays where the actual element
order is significant.
arsort example
$fruits = array("d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple");
arsort($fruits);
for(reset($fruits); $key = key($fruits); next($fruits)) {
echo "fruits[$key] = ".$fruits[$key]."\n";
}
This example would display:
fruits[a] = orange
fruits[d] = lemon
fruits[b] = banana
fruits[c] = apple
The fruits have been sorted in reverse alphabetical order, and
the index associated with each element has been maintained.
See also: asort, rsort,
ksort, and sort.
asort
Sort an array and maintain index association
Description
void asort
array array
This function sorts an array such that array indices maintain their
correlation with the array elements they are associated with. This is
used mainly when sorting associative arrays where the actual element
order is significant.
asort example
$fruits = array("d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple");
asort($fruits);
for(reset($fruits); $key = key($fruits); next($fruits)) {
echo "fruits[$key] = ".$fruits[$key]."\n";
}
This example would display:
fruits[c] = apple
fruits[b] = banana
fruits[d] = lemon
fruits[a] = orange
The fruits have been sorted in alphabetical order, and the index
associated with each element has been maintained.
See also arsort, rsort,
ksort, and sort.
count
count elements in a variable
Description
int count
mixed var
Returns the number of elements in var, which is
typically an array (since anything else will have one element).
Returns 1 if the variable is not an array.
Returns 0 if the variable is not set.
count may return 0 for a variable that isn't
set, but it may also return 0 for a variable that has been initialized
with an empty array. Use isset to test if a
variable is set.
See also:
sizeof, isset, and
is_array.
current
Return the current element in an array
Description
mixed current
array array
Every array has an internal pointer to its "current" element,
which is initialized to the first element inserted into the
array.
The current function simply returns the
array element that's currently being pointed by the internal
pointer. It does not move the pointer in any way. If the
internal pointer points beyond the end of the elements list,
current returns false.
If the array contains empty elements (0 or "", the empty string)
then this function will return false for these elements as well.
This makes it impossible to determine if you are really at the
end of the list in such an array using current.
To properly traverse an array that may contain empty elements,
use the each function.
See also:
end, next,
prev and reset.
each
Return the next key and value pair from an array
Description
array each
array array
Returns the current key and value pair from the array
array and advances the array
cursor. This pair is returned in a four-element array,
with the keys 0, 1,
key, and value. Elements
0 and key contain
the key name of the array element, and 1
and value contain the data.
If the internal pointer for the array points past the end of the
array contents, each returns false.
each examples
$foo = array( "bob", "fred", "jussi", "jouni" );
$bar = each( $foo );
$bar now contains the following key/value
pairs:
0 => 0
1 => 'bob'
key => 0
value => 'bob'
$foo = array( "Robert" => "Bob", "Seppo" => "Sepi" );
$bar = each( $foo );
$bar now contains the following key/value
pairs:
0 => 'Robert'
1 => 'Bob'
key => 'Robert'
value => 'Bob'
each is typically used in conjunction with
list to traverse an array; for instance,
$HTTP_POST_VARS:
Traversing $HTTP_POST_VARS with each
echo "Values submitted via POST method:<br>";
while (list($key, $val) = each($HTTP_POST_VARS)) {
echo "$key => $val<br>";
}
After each has executed, the array cursor
will be left on the next element of the array, or on the last
element if it hits the end of the array.
See also key, list,
current, reset,
next, and prev.
end
Set the internal pointer of an array to its last element
Description
end
array array
end advances array's
internal pointer to the last element.
See also:
current, each, end
next and reset
key
Fetch a key from an associative array
Description
mixed key
array array
key returns the index element of the
current array position.
See also:
current,
next
ksort
Sort an array by key
Description
int ksort
array array
Sorts an array by key, maintaining key to data correlations. This
is useful mainly for associative arrays.
ksort example
$fruits = array("d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple");
ksort($fruits);
for(reset($fruits); $key = key($fruits); next($fruits)) {
echo "fruits[$key] = ".$fruits[$key]."\n";
}
This example would display:
fruits[a] = orange
fruits[b] = banana
fruits[c] = apple
fruits[d] = lemon
See also asort, arsort,
sort, and rsort.
list
Assign variables as if they were an array
Description
void list
Like array, this is not really a function,
but a language construct. list is used to
assign a list of variables in one operation.
list example
<table>
<tr>
<th>Employee name</th>
<th>Salary</th>
</tr>
<?php
$result = mysql($conn, "SELECT id, name, salary FROM employees");
while (list($id, $name, $salary) = mysql_fetch_row($result)) {
print(" <tr>\n".
" <td><a href=\"info.php3?id=$id\">$name</a></td>\n".
" <td>$salary</td>\n".
" </tr>\n");
}
?></table>
See also: each, array.
next
Advance the internal array pointer of an array
Description
mixed next
array array
Returns the array element in the next place that's pointed by the
internal array pointer, or false if there are no more elements.
next behaves like current,
with one difference. It advances the internal array pointer one
place forward before returning the element. That means it returns
the next array element and advances the internal array pointer by
one. If advancing the internal array pointer results in going beyond
the end of the element list, next returns false.
If the array contains empty elements then this function will return
false for these elements as well. To properly traverse an array
which may contain empty elements see the each
function.
See also:
current, end
prev and reset
pos
Get the current element from an array
Description
mixed pos
array array
This is an alias for current.
See also:
end, next,
prev and reset.
prev
Rewind the internal array pointer
Description
mixed prev
array array
Returns the array element in the previous place that's pointed by
the internal array pointer, or false if there are no more elements.
If the array contains empty elements then this function will return
false for these elements as well. To properly traverse an array
which may contain empty elements see the each
function.
prev behaves just like
next, except it rewinds the internal array
pointer one place instead of advancing it.
See also:
current, end
next and reset
range
Create an array containing a range of integers
Description
array range
int low
int high
range returns an array of integers from
low to high,
inclusive.
See shuffle for an example of its use.
reset
Set the internal pointer of an array to its first element
Description
mixed reset
array array
reset rewinds array's
internal pointer to the first element.
reset returns the value of the first array
element.
See also:
current, each, next
prev and reset
rsort
Sort an array in reverse order
Description
void rsort
array array
This function sorts an array in reverse order (highest to lowest).
rsort example
$fruits = array("lemon","orange","banana","apple");
rsort($fruits);
for (reset($fruits); list($key,$value) = each($fruits); ) {
echo "fruits[$key] = ", $value, "\n";
}
This example would display:
fruits[0] = orange
fruits[1] = lemon
fruits[2] = banana
fruits[3] = apple
The fruits have been sorted in reverse alphabetical order.
See also arsort, asort,
ksort, sort and usort.
shuffle
Shuffle an array
Description
void shuffle
array array
This function shuffles (randomizes the order of the elements in)
an array.
shuffle example
$numbers = range(1,20);
srand(time());
shuffle($numbers);
while (list(,$number) = each($numbers)) {
echo "$number ";
}
See also arsort, asort,
ksort, rsort,
sort and usort.
sizeof
Get the number of elements in an array
Description
int sizeof
array array
Returns the number of elements in the array.
See also:
count
sort
Sort an array
Description
void sort
array array
This function sorts an array. Elements will be arranged from lowest to
highest when this function has completed.
sort example
$fruits = array("lemon","orange","banana","apple");
sort($fruits);
for(reset($fruits); $key = key($fruits); next($fruits)) {
echo "fruits[$key] = ".$fruits[$key]."\n";
}
This example would display:
fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange
The fruits have been sorted in alphabetical order.
See also arsort, asort,
ksort, rsort, and usort.
uasort
Sort an array with a user-defined comparison function and maintain index association
Description
void uasort
array array
function cmp_function
This function sorts an array such that array indices maintain their
correlation with the array elements they are associated with. This is
used mainly when sorting associative arrays where the actual element
order is significant. The comparison function is user-defined.
uksort
Sort an array by keys using a user-defined comparison function
Description
void uksort
array array
function cmp_function
This function will sort the keys of an array using a
user-supplied comparison function. If the array you wish to sort
needs to be sorted by some non-trivial criteria, you should use
this function.
uksort example
function mycompare($a, $b) {
if ($a == $b) return 0;
return ($a > $b) ? -1 : 1;
}
$a = array(4 => "four", 3 => "three", 20 => "twenty", 10 => "ten");
uksort($a, mycompare);
while(list($key, $value) = each($a)) {
echo "$key: $value\n";
}
This example would display:
20: twenty
10: ten
4: four
3: three
See also arsort, asort,
uasort, ksort,
rsort and sort.
usort
Sort an array by values using a user-defined comparison function
Description
void usort
array array
function cmp_function
This function will sort an array by its values using a
user-supplied comparison function. If the array you wish to sort
needs to be sorted by some non-trivial criteria, you should use
this function.
The comparison function must return an integer less than, equal to, or
greater than zero if the first argument is considered to be respectively
less than, equal to, or greater than the second. If two members compare
as equal, their order in the sorted array is undefined.
usort example
function cmp($a,$b) {
if ($a == $b) return 0;
return ($a > $b) ? -1 : 1;
}
$a = array(3,2,5,6,1);
usort($a, cmp);
while(list($key,$value) = each($a)) {
echo "$key: $value\n";
}
This example would display:
0: 6
1: 5
2: 3
3: 2
4: 1
Obviously in this trivial case the rsort function
would be more appropriate.
The underlying quicksort function in some C libraries (such as on
Solaris systems) may cause PHP to crash if the comparison function
does not return consistent values.
See also:
arsort, asort,
ksort, rsort and
sort.