0% found this document useful (0 votes)
26 views

Array Functions

Uploaded by

kamrul hasan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Array Functions

Uploaded by

kamrul hasan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Function Description Implementation

array_unshift Prepend one or more elements to array_unshift ( array &$array [, mixed $... ] ) : int
the beginning of an array

array_push Push one or more elements onto array_push ( array &$array [, mixed $... ] ) : int
the end of array

array_shift Shift an element off the beginning array_shift ( array &$array ) : mixed
of array

array_pop Pop the element off the end of array_pop ( array &$array ) : mixed
array

array_change_key_case Changes the case of all keys in an array_change_key_case ( array $array [, int $case = CASE_LOWER ] ) :
array array

array_chunk Split an array into chunks array_chunk ( array $array , int $size [, bool $preserve_keys = FALSE ] ) :
array

array_slice Extract a slice of the array array_slice ( array $array , int $offset [, int $length = NULL [, bool
$preserve_keys = FALSE ]] ) : array
$input = array("a", "b", "c", "d", "e");

$output = array_slice($input, 2); // returns "c", "d", and "e"


$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3); // returns "a", "b", and "c"

array_splice Remove a portion of the array and array_splice ( array &$input , int $offset [, int $length = count($input) [,
replace it with something else mixed $replacement = array() ]] ) : array
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
Array
(

1
[0] => red
[1] => yellow
)

array_column Return the values from a single array_column ( array $input , mixed $column_key [, mixed $index_key =
column in the input array NULL ] ) : array

array_combine Creates an array by using one array_combine ( array $keys , array $values ) : array
array for keys and another for its
values

array_search Searches the array for a given array_search ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) :
value and returns the first mixed
corresponding key if successful

array_values Return all the values of an array array_values ( array $array ) : array
$array = array("size" => "XL", "color" => "gold");
print_r(array_values($array));
Array
(
[0] => XL
[1] => gold
)

array_keys Return all the keys or a subset of array_keys ( array $array ) : array
the keys of an array array_keys ( array $array , mixed $search_value [, bool $strict = FALSE ] ) :
array
$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));
Array
(
[0] => 0
[1] => 3
[2] => 4
)

2
array_key_first Gets the first key of an array array_key_first ( array $array ) : mixed

array_key_last Gets the last key of an array array_key_last ( array $array ) : mixed

array_key_exists/key_exi Checks if the given key or index array_key_exists ( mixed $key , array $array ) : bool
sts [Alias] exists in the array

array_flip Exchanges all keys with their array_flip ( array $array ) : array
associated values in an array $input = array("oranges", "apples", "pears");
$flipped = array_flip($input);
Array
(
[oranges] => 0
[apples] => 1
[pears] => 2
)

array_pad Pad array to the specified length array_pad ( array $array , int $size , mixed $value ) : array
with a value $input = array(12, 10, 9);
$result = array_pad($input, 5, 0); // result is array(12, 10, 9, 0, 0)
$result = array_pad($input, -7, -1);// result is array(-1, -1, -1, -1, 12, 10, 9)

array_fill Fills an array with num entries of array_fill ( int $start_index , int $num , mixed $value ) : array
the value of the value parameter, $a = array_fill(5, 2, 'banana');
keys starting at the start_index Array
parameter. (
[5] => banana
[6] => banana
)

array_fill_keys Fill an array with values, specifying array_fill_keys ( array $keys , mixed $value ) : array
keys $keys = array('foo', 5, 10, 'bar');
$a = array_fill_keys($keys, 'banana');
print_r($a);
Array

3
(
[foo] => banana
[5] => banana
[10] => banana
[bar] => banana
)

range Create an array containing a range range ( mixed $start , mixed $end [, number $step = 1 ] ) : array
of elements

array_count_values Counts all the values of an array array_count_values ( array $array ) : array
array_count_values([1, "hello", 1, "world", "hello"]);
Array
(
[1] => 2
[hello] => 2
[world] => 1
)

array_diff Computes the difference of arrays. array_diff ( array $array1 , array $array2 [, array $... ] ) : array
Compares array1 against one or $array1 = array("a" => "green", "red", "blue", "red");
more other arrays and returns the $array2 = array("b" => "green", "yellow", "red");
values in array1 that are not $result = array_diff($array1, $array2);
present in any of the other arrays. Array
(
[1] => blue
)

array_diff_assoc Computes the difference of arrays array_diff_assoc ( array $array1 , array $array2 [, array $... ] ) : array
with additional index check $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
[In this example you see the "a" => $result = array_diff_assoc($array1, $array2);
"green" pair is present in both print_r($result);
arrays and thus it is not in the Array
output from the function. Unlike (

4
this, the pair 0 => "red" is in the [b] => brown
output because in the second [c] => blue
argument "red" has key which is [0] => red
1.] )

array_diff_key Computes the difference of arrays array_diff_key ( array $array1 , array $array2 [, array $... ] ) : array
using keys for comparison $array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'yellow' => 7, 'cyan' => 8);
print_r(array_diff_key($array1, $array2));
Array
(
[blue] => 1
[red] => 2
[purple] => 4
)

array_diff_uassoc Computes the difference of arrays array_diff_uassoc ( array $array1 , array $array2 [, array $... ], callable
with additional index check which $key_compare_func ) : array
is performed by a user supplied $array1 = ["a" => "green", "b" => "brown", "c" => "blue", "red"];
callback function. $array2 = ["a" => "green", "yellow", "red"];
$result = array_diff_uassoc($array1, $array2, function($a, $b) {
Compares array1 against array2 if ($a === $b) {
and returns the difference. Unlike return 0;
array_diff() the array keys are }
used in the comparison. return ($a > $b)? 1:-1;
});
Unlike array_diff_assoc() a user Array
supplied callback function is used (
for the indices comparison, not [b] => brown
internal function. [c] => blue
[0] => red
)

array_diff_ukey Computes the difference of arrays array_diff_ukey ( array $array1 , array $array2 [, array $... ], callable
using a callback function on the $key_compare_func ) : array

5
keys for comparison $array1 = ['blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4];
$array2 = ['green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8];
Compares the keys from array1 $result = array_diff_ukey($array1, $array2, function($key1, $key2){
against the keys from array2 and if ($key1 === $key2) {
returns the difference. This return 0;
function is like array_diff() except }
the comparison is done on the return ($key1 > $key2)? 1:-1;
keys instead of the values. });
Array
Unlike array_diff_key() a user (
supplied callback function is used [red] => 2
for the indices comparison, not [purple] => 4
internal function. )

array_udiff Computes the difference of arrays array_udiff ( array $array1 , array $array2 [, array $... ], callable
by using a callback function for $value_compare_func ) : array
data comparison. Computes the
difference of arrays by using a $arr1 = [['Bob', 42], ['Phil', 37], ['Frank', 39]];
callback function for data $arr2 = [['Phil', 37], ['Mark', 45]];
comparison. This is unlike $result = array_udiff($arr1, $arr2, function($a,$b) {
array_diff() which uses an internal return strcmp( implode("", $a), implode("", $b) );
function for comparing the data. });
Array
(
[0] => Array
(
[0] => Bob
[1] => 42
)

[2] => Array


(
[0] => Frank
[1] => 39

6
)

array_udiff_assoc Computes the difference of arrays array_udiff_assoc ( array $array1 , array $array2 [, array $... ], callable
with additional index check, $value_compare_func ) : array
compares data by a callback
function. Please note that this
function only checks one
dimension of a n-dimensional
array. Of course you can check
deeper dimensions by using, for
example,
array_udiff_assoc($array1[0],
$array2[0],
"some_comparison_func");.

array_udiff_uassoc Computes the difference of arrays array_udiff_uassoc ( array $array1 , array $array2 [, array $... ], callable
with additional index check, $value_compare_func , callable $key_compare_func ) : array
compares data and indexes by a
callback function

array_intersect Computes the intersection of array_intersect ( array $array1 , array $array2 [, array $... ] ) : array
arrays $array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
Array
(
[a] => green
[0] => red
)

array_intersect_assoc Computes the intersection of array_intersect_assoc ( array $array1 , array $array2 [, array $... ] ) : array
arrays with additional index check $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "b" => "yellow", "blue", "red");

7
$result_array = array_intersect_assoc($array1, $array2);
Array
(
[a] => green
)

array_intersect_key Computes the intersection of array_intersect_key ( array $array1 , array $array2 [, array $... ] ) : array
arrays using keys for comparison $array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
print_r(array_intersect_key($array1, $array2));
Array
(
[blue] => 1
[green] => 3
)

array_intersect_uassoc Computes the intersection of array_intersect_uassoc ( array $array1 , array $array2 [, array $... ],
arrays with additional index check, callable $key_compare_func ) : array
compares indexes by a callback $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
function $array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");
print_r(array_intersect_uassoc($array1, $array2, "strcasecmp"));
Array
(
[b] => brown
)

array_intersect_ukey Computes the intersection of array_intersect_ukey ( array $array1 , array $array2 [, array $... ], callable
arrays using a callback function on $key_compare_func ) : array
the keys for comparison $array1 = ['blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4];
$array2 = ['green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8];
$result = array_intersect_ukey($array1, $array2, function($key1, $key2){
if ($key1 === $key2) {
return 0;
}
return ($key1 > $key2)? 1:-1;

8
});
Array
(
[blue] => 1
[green] => 3
)

array_uintersect Computes the intersection of array_uintersect ( array $array1 , array $array2 [, array $... ], callable
arrays, compares data by a $value_compare_func ) : array
callback function $arr1 = [['Bob', 42], ['Phil', 37], ['Frank', 39]];
$arr2 = [['Phil', 37], ['Mark', 45]];
$result = array_uintersect($arr1, $arr2, function($a,$b) {
return strcmp( implode("", $a), implode("", $b) );
});
Array
(
[1] => Array
(
[0] => Phil
[1] => 37
)

array_uintersect_assoc Computes the intersection of array_uintersect_assoc ( array $array1 , array $array2 [, array $... ],
arrays with additional index check, callable $value_compare_func ) : array
compares data by a callback
function. $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
Computes the intersection of $array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");
arrays with additional index check,
compares data by a callback print_r(array_uintersect_assoc($array1, $array2, "strcasecmp"));
function. Array
(
Note that the keys are used in the [a] => green

9
comparison unlike in )
array_uintersect(). The data is
compared by using a callback
function.

array_uintersect_uassoc Computes the intersection of array_uintersect_uassoc ( array $array1 , array $array2 [, array $... ],
arrays with additional index check, callable $value_compare_func , callable $key_compare_func ) : array
compares data and indexes by
separate callback functions $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");
print_r(array_uintersect_uassoc($array1, $array2, "strcasecmp",
"strcasecmp"));
Array
(
[a] => green
[b] => brown
)

array_filter Filters elements of an array using array_filter ( array $array [, callable $callback [, int $flag = 0 ]] ) : array
a callback function

array_map Applies the callback to the array_map ( callable $callback , array $array1 [, array $... ] ) : array
elements of the given arrays.
array_map() returns an array
containing the results of applying
the callback to the corresponding
index of array1 (and ... if more
arrays are provided) used as
arguments for the callback. The
number of parameters that the
callback function accepts should
match the number of arrays
passed to array_map().

array_walk Apply a user supplied function to array_walk ( array &$array , callable $callback [, mixed $userdata = NULL ]

10
every member of an array ) : bool

array_walk_recursive Apply a user function recursively array_walk_recursive ( array &$array , callable $callback [, mixed
to every member of an array $userdata = NULL ] ) : bool

array_merge Merge one or more arrays. array_merge ([ array $... ] ) : array


array_merge() causes all numeric
keys found in the input arrays to
be reindexed in the resultant array.
The union operator + does not
cause a reindex.

array_merge_recursive Merge one or more arrays array_merge_recursive ([ array $... ] ) : array


recursively

array_replace Replaces elements from passed array_replace ( array $array1 [, array $... ] ) : array
arrays into the first array $base = array("orange", "banana", "apple", "raspberry");
$replacements = array(0 => "pineapple", 4 => "cherry");
$replacements2 = array(0 => "grape");
$basket = array_replace($base, $replacements, $replacements2);
print_r($basket);
Array
(
[0] => grape
[1] => banana
[2] => apple
[3] => raspberry
[4] => cherry
)

array_replace_recursive Replaces elements from passed array_replace_recursive ( array $array1 [, array $... ] ) : array
arrays into the first array
recursively

array_sum Calculate the sum of values in an array_sum ( array $array ) : number

11
array

array_product Calculate the product of values in array_product ( array $array ) : number


an array

array_reduce Iteratively reduce the array to a array_reduce ( array $array , callable $callback [, mixed $initial = NULL ] ) :
single value using a callback mixed
function $a = array(1, 2, 3, 4, 5);
var_dump(array_reduce($a, function ($carry, $item)
{
$carry *= $item;
return $carry;
}, 10)); // int(1200), because: 10*1*2*3*4*5

array_rand Pick one or more random keys out array_rand ( array $array [, int $num = 1 ] ) : mixed
of an array

array_reverse Return an array with elements in array_reverse ( array $array [, bool $preserve_keys = FALSE ] ) : array
reverse order

array_multisort Sort multiple or multi-dimensional array_multisort ( array &$array1 [, mixed $array1_sort_order = SORT_ASC
arrays [, mixed $array1_sort_flags = SORT_REGULAR [, mixed $... ]]] ) : boo

shuffle Shuffle an array shuffle ( array &$array ) : bool

sort Sort an array sort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) : bool

rsort Sort an array in reverse order rsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) : bool

asort Sort an array and maintain index asort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) : bool
association

arsort Sort an array in reverse order and arsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) : bool
maintain index association

12
ksort Sort an array by key ksort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) : bool

krsort Sort an array by key in reverse krsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) : bool
order

usort Sort an array by values using a usort ( array &$array , callable $value_compare_func ) : bool
user-defined comparison function

uasort Sort an array with a user-defined uasort ( array &$array , callable $value_compare_func ) : bool
comparison function and maintain
index association

uksort Sort an array by keys using a uksort ( array &$array , callable $key_compare_func ) : bool
user-defined comparison function

natsort Sort an array using a "natural natsort ( array &$array ) : bool


order" algorithm

natcasesort Sort an array using a case natcasesort ( array &$array ) : bool


insensitive "natural order"
algorithm

compact Create array containing variables compact ( mixed $varname1 [, mixed $... ] ) : array
and their values

extract Import variables into the current extract ( array &$array [, int $flags = EXTR_OVERWRITE [, string $prefix =
symbol table from an array NULL ]] ) : int

list Assign variables as if they were an list ( mixed $var1 [, mixed $... ] ) : array
array

current/pos[Alias] Return the current element in an current ( array $array ) : mixed


array

next Advance the internal pointer of an next ( array &$array ) : mixed

13
array. next() behaves like
current(), with one difference. It
advances the internal array pointer
one place forward before returning
the element value.

prev Rewind the internal array pointer. prev ( array &$array ) : mixed
prev() behaves just like next(),
except it rewinds the internal array
pointer one place instead of
advancing it.

end Set the internal pointer of an array end ( array &$array ) : mixed
to its last element

key Fetch a key from an array. key() key ( array $array ) : mixed
returns the index element of the
current array position.

reset Set the internal pointer of an array reset ( array &$array ) : mixed
to its first element

count/sizeof [Alias] Count all elements in an array, or count ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] ) :
something in an object int

in_array Checks if a value exists in an array in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) : bool

14

You might also like