You are on page 1of 54

PHP Notes By Deepak Mitra

1

PHP Array Functions
These functions allow you to interact with and manipulate arrays in various ways. Arrays are
essential for storing, managing, and operating on sets of variables.
Installation:
There is no installation needed to use these functions; they are part of the PHP core.
Runtime Configuration:
This extension has no configuration directives defined in php.ini.
PHP Array Constants:
Constant Description
CASE_LOWER Used with array_change_key_case() to convert array keys to
lower case
CASE_UPPER Used with array_change_key_case() to convert array keys to
upper case
SORT_ASC Used with array_multisort() to sort in ascending order
SORT_DESC Used with array_multisort() to sort in descending order
SORT_REGULAR Used to compare items normally
SORT_NUMERIC Used to compare items numerically
SORT_STRING Used to compare items as strings
SORT_LOCALE_STRING Used to compare items as strings, based on the current locale
COUNT_NORMAL
COUNT_RECURSIVE
EXTR_OVERWRITE
EXTR_SKIP
EXTR_PREFIX_SAME
EXTR_PREFIX_ALL
EXTR_PREFIX_INVALID
EXTR_PREFIX_IF_EXISTS
EXTR_IF_EXISTS
EXTR_REFS

PHP Notes By Deepak Mitra
2

List of Functions:
PHP: indicates the earliest version of PHP that supports the function.
Function Description PHP
array() Create an array 3
array_change_key_case() Returns an array with all keys in lowercase or
uppercase
4
array_chunk() Splits an array into chunks of arrays 4
array_combine() Creates an array by using one array for keys and
another for its values
5
array_count_values() Returns an array with the number of occurrences for
each value
4
array_diff() Compares array values, and returns the differences 4
array_diff_assoc() Compares array keys and values, and returns the
differences
4
array_diff_key() Compares array keys, and returns the differences 5
array_diff_uassoc() Compares array keys and values, with an additional
user-made function check, and returns the differences
5
array_diff_ukey() Compares array keys, with an additional user-made
function check, and returns the differences
5
array_fill() Fills an array with values 4
array_fill_keys() Fill an array with values, specifying keys 5
array_filter() Filters elements of an array using a user-made
function
4
array_flip() Exchanges all keys with their associated values in an
array
4
array_intersect() Compares array values, and returns the matches 4
array_intersect_assoc() Compares array keys and values, and returns the
matches
4
array_intersect_key() Compares array keys, and returns the matches 5
array_intersect_uassoc() Compares array keys and values, with an additional
user-made function check, and returns the matches
5
array_intersect_ukey() Compares array keys, with an additional user-made
function check, and returns the matches
5
array_key_exists() Checks if the specified key exists in the array 4
array_keys() Returns all the keys of an array 4
PHP Notes By Deepak Mitra
3

array_map() Sends each value of an array to a user-made
function, which returns new values
4
array_merge() Merges one or more arrays into one array 4
array_merge_recursive() Merges one or more arrays into one array 4
array_multisort() Sorts multiple or multi-dimensional arrays 4
array_pad() Inserts a specified number of items, with a specified
value, to an array
4
array_pop() Deletes the last element of an array 4
array_product() Calculates the product of the values in an array 5
array_push() Inserts one or more elements to the end of an array 4
array_rand() Returns one or more random keys from an array 4
array_reduce() Returns an array as a string, using a user-defined
function
4
array_reverse() Returns an array in the reverse order 4
array_search() Searches an array for a given value and returns the
key
4
array_shift() Removes the first element from an array, and returns
the value of the removed element
4
array_slice() Returns selected parts of an array 4
array_splice() Removes and replaces specified elements of an array 4
array_sum() Returns the sum of the values in an array 4
array_udiff() Compares array values in a user-made function and
returns an array
5
array_udiff_assoc() Compares array keys, and compares array values in a
user-made function, and returns an array
5
array_udiff_uassoc() Compares array keys and array values in user-made
functions, and returns an array
5
array_uintersect() Compares array values in a user-made function and
returns an array
5
array_uintersect_assoc() Compares array keys, and compares array values in a
user-made function, and returns an array
5
array_uintersect_uassoc() Compares array keys and array values in user-made
functions, and returns an array
5
array_unique() Removes duplicate values from an array 4
array_unshift() Adds one or more elements to the beginning of an
array
4
PHP Notes By Deepak Mitra
4

array_values() Returns all the values of an array 4
array_walk() Applies a user function to every member of an array 3
array_walk_recursive() Applies a user function recursively to every member
of an array
5
arsort() Sorts an array in reverse order and maintain index
association
3
asort() Sorts an array and maintain index association 3
compact() Create array containing variables and their values 4
count() Counts elements in an array, or properties in an
object
3
current() Returns the current element in an array 3
each() Returns the current key and value pair from an array 3
end() Sets the internal pointer of an array to its last
element
3
extract() Imports variables into the current symbol table from
an array
3
in_array() Checks if a specified value exists in an array 4
key() Fetches a key from an array 3
krsort() Sorts an array by key in reverse order 3
ksort() Sorts an array by key 3
list() Assigns variables as if they were an array 3
natcasesort() Sorts an array using a case insensitive "natural order"
algorithm
4
natsort() Sorts an array using a "natural order" algorithm 4
next() Advance the internal array pointer of an array 3
pos() Alias of current() 3
prev() Rewinds the internal array pointer 3
range() Creates an array containing a range of elements 3
reset() Sets the internal pointer of an array to its first
element
3
rsort() Sorts an array in reverse order 3
shuffle() Shuffles an array 3
sizeof() Alias of count() 3
sort() Sorts an array 3
PHP Notes By Deepak Mitra
5

uasort() Sorts an array with a user-defined function and
maintain index association
3
uksort() Sorts an array by keys using a user-defined function 3
usort() Sorts an array by values using a user-defined function 3
array()
Syntax
array(key1 => value1, key2 => value2...)
Definition and Usage
Returns an array of the parameters. The parameters can be given an index with the =>
operator
Paramters
Parameter Description
key
Optional. Specifies the key, of type numeric or string. If not set, an integer key
is generated, starting at 0
value Required. Specifies the value
Return Values
Returns an array of the parameters.
Example
Try out following example
<?php
$a=array("a"=>"Dog", "b"=>"Cat", "c"=>"Horse");
print_r($a);
?>
This will produce following result:
Array ( [a] => Dog [b] => Cat [c] => Horse )
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.

PHP Notes By Deepak Mitra
6

<?php

$fruits = array (
"fruits" => array("a"=>orange", "b"=>banana", "c"=>apple"),
"numbers" => array(1, 2, 3, 4, 5, 6),
"holes" => array("first", 5 => "second", "third")
);
?>
array_change_key_case()
Syntax
array array_change_key_case ( array $input [, int $case] )
Definition and Usage
Returns an array with all keys from input lowercased or uppercased. Numbered indices are
left as is.
Paramters
Parameter Description
input The array to work on
case Either CASE_UPPER or CASE_LOWER (default)
Return Values
Returns an array with its keys lower or uppercased, or false if input is not an array.
Example
Try out following example:
<?php
$input_array = array("FirSt" => 1, "SecOnd" => 4);
print_r(array_change_key_case($input_array, CASE_UPPER));
?>
This will produce following result:
Array ( [FIRST] => 1 [SECOND] => 4 )
array_chunk()
Syntax
PHP Notes By Deepak Mitra
7

array array_chunk ( array $input, int $size [, bool $preserve_keys] );
Definition and Usage
Chunks an array into size large chunks. The last chunk may contain less
than size elements.
Paramters
Parameter Description
input The array to work on
size The size of each chunk
preserve_keys
When set to TRUE keys will be preserved. Default is FALSE which will
reindex the chunk numerically
Return Values
Returns a multidimensional numerically indexed array, starting with zero, with each
dimension containing size elements.
Example
Try out following example:
<?php
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));
?>
This will produce following result:
Array (
[0] => Array
(
[0] => a
[1] => b
)

[1] => Array
(
[0] => c
[1] => d
)

[2] => Array
(
PHP Notes By Deepak Mitra
8

[0] => e
)

)
Array
(
[0] => Array
(
[0] => a
[1] => b
)

[1] => Array
(
[2] => c
[3] => d
)

[2] => Array
(
[4] => e
)

)
array_combine()
Syntax
array array_combine ( array $keys, array $values );
Definition and Usage
Creates an array by using the values from the keys array as keys and the values from
the values array as the corresponding values.
Paramters
Parameter Description
keys Array of keys to be used
values Array of values to be used
Return Values
Returns the combined array, FALSE if the number of elements for each array isn't equal or
if the arrays are empty.
Example
PHP Notes By Deepak Mitra
9

Try out following example:
<?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

print_r($c);
?>
This will produce following result:
Array([green] => avocado [red] => apple [yellow] => banana)
array_count_values()
Syntax
array array_count_values ( array $input );
Definition and Usage
Returns an array using the values of the input array as keys and their frequency in input as
values.
Paramters
Parameter Description
input The array of values to count
Return Values
Returns an associative array of values from input as keys and their count as value.
Example
Try out following example:
<?php
$input_array = array("orange", "mango", "banan", "orange" );
print_r(array_count_values($input_array));
?>
This will produce following result:
Array ( [orange] => 2 [mango] => 1 [banana => 1 )
PHP Notes By Deepak Mitra
10

array_diff()
Syntax
array array_diff ( array $array1, array $array2 [, array $array3 ...] );
Definition and Usage
Compares array1 against array2 and returns the difference.
Paramters
Parameter Description
array1 Required. The first array is the array that the others will be compared with.
array2 Required. An array to be compared with the first array
array3 Optional. An array to be compared with the first array
Return Values
Returns an array containing the differences.
Example
Try out following example:
<?php
$input_array1 = array("orange", "banana", "apple");
$input_array2 = array("orange", "mango", "apple");
print_r(array_diff($input_array1, $input_array2));
?>
This will produce following result:
Array ( [1] => banana )
array_diff_ukey()
Syntax
array_diff_ukey ( $array1, $array2 [, $array3...,callback $key_compare_func] );
Definition and Usage
Compares the keys from array1 against the keys from array2 and returns the difference.
This function is like array_diff() except the comparison is done on the keys instead of the
values.
PHP Notes By Deepak Mitra
11

Unlike array_diff_key() an user supplied callback function is used for the indices
comparision, not internal function.
Paramters
Parameter Description
array1
Required. The first array is the array that the others will be compared
with.
array2 Required. An array to be compared with the first array
array3 Optional. An array to be compared with the first array
key_compare_func
Required. callback function to use. The callback 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.
Return Values
Returns an array containing all the entries from array1 that are not present in any of the
other arrays.
Example
Try out following example:
<?php
function key_compare_func($v1,$v2)
{
if ($v1===$v2)
{
return 0;
}
if ($v1>v2)
{
return 1;
}
else
{
return -1;
}
}
$array1 = array(0=>"banana", 1=>"orange", 2=>"grapes");
$array2 = array(3=>"apple",1=>"apricot", 5=>"mango");
print_r(array_diff_ukey($array1,$array2,"key_compare_func"));
?>
This will produce following result:
Array ( [0]=>banana [2]=>grapes )
PHP Notes By Deepak Mitra
12

array_diff_uassoc()
Syntax
array_diff_uassoc ( $array1, $array2 [, $array3..., callback $key_compare_func] );
Definition and Usage
Compares array1 against array2 and returns the difference. Unlike array_diff() the array
keys are used in the comparison.
Unlike array_diff_assoc() an user supplied callback function is used for the indices
comparison, not internal function.
Parameters
Parameter Description
array1 Required. The array to compare from
array2 Required. An array to be compared with the first array
array3 Optional. An array to be compared with the first array
key_compare_func
Required. callback function to use. The callback 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.
Return Values
Returns an array containing all the entries from array1 that are not present in any of the
other arrays.
Example
Try out following example:
<?php
function key_compare_func($a, $b)
{
if ($a === $b) {
return 0;
}
return ($a > $b)? 1:-1;
}
$array1 = array("a" => "green", "b" => "brown",
"c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_uassoc($array1, $array2, "key_compare_func");
print_r($result);
PHP Notes By Deepak Mitra
13


?>
This will produce following result:
Array ( [b] => brown [c] => blue [0] => red )
array_diff_key()
Syntax
array array_diff_key ( array $array1, array $array2 [, array $...] );
Definition and Usage
Compares array1 against array2 and returns the difference.
Parameters
Parameter Description
array1 Required. The first array is the array that the others will be compared with.
array2 Required. An array to be compared with the first array
array3 Optional. An array to be compared with the first array
Return Values
Returns an array containing all the entries from array1 that are not present in any of the
other arrays.
Example
Try out following example:
<?php
$array1 = array('blue' => 1, 'red' => 2, 'purple' => 3);

$array2 = array('blue' => 4, 'yellow' => 5, 'cyan' => 6);

var_dump(array_diff($input_array1, $input_array2));
?>
This will produce following result:
array(2) {
["red"]=>
PHP Notes By Deepak Mitra
14

int(2)
["purple"]=>
int(3)
}
array_diff_assoc()
Syntax
array array_diff_assoc( array $array1, array $array2 [, array $array3...] );
Definition and Usage
Compares array1 against array2 and returns the difference. Unlike array_diff() the array
keys are used in the comparison.
Parameters
Parameter Description
array1 Required. The array to compare from
array2 Required. An array to be compared with the first array
array3 Optional. An array to be compared with the first array
Return Values
Returns an array containing all the values from array1 that are not present in any of the
other arrays with the same keys.
Example
Try out following example:
<?php
$input_array1 = array( a=>"orange", b=>"mango", c=>"banana");
$input_array2 = array( a=>"orange", b=>"apple", c=>"banana");
print_r(array_diff_assoc($input_array1, $input_array2));

$input_array1 = array( a=>"orange", b=>mango", c=>"banana");
$input_array2 = array( a=>"banana", b=>"apple", c=>"orange");
print_r(array_diff_assoc($input_array1, $input_array2));
?>
This will produce following result:
Array ( [b] => mango )
Array ( [a] => orange [b] => mango [c] => banana )
PHP Notes By Deepak Mitra
15

array_fill()
Syntax
array array_fill ( int $start_index, int $num, mixed $value );
Definition and Usage
Fills an array with num entries of the value of the value parameter, keys starting at
the start_index parameter.
Parameters
Parameter Description
start_index The first index of the returned array
num Number of elements to insert
value Values to use filling
Return Values
Returns the filled array
Example
Try out following example:
<?php
$a = array_fill(5, 6, 'apple');
print_r($a)
?>
This will produce following result:
Array
(
[5] => apple
[6] => apple
[7] => apple
[8] => apple
[9] => apple
[10] => apple
)
array_fill_keys()
Syntax
PHP Notes By Deepak Mitra
16

array array_fill_keys ( array $keys, mixed $value );
Definition and Usage
Fills an array with the value of the value parameter, using the values of the keys array as
keys.
Parameters
Parameter Description
keys Array of values that will be used as keys
value Either an string or an array of values
Return Values
Returns the filled array
Example
Try out following example:
<?php
$keys = array('foo', 5, 10, 'bar');
$a = array_fill_keys($keys, 'banana');
print_r($a)
?>
This will produce following result:
Array
(
[foo] => banana
[5] => banana
[10] => banana
[bar] => banana
)
array_filter()
Syntax
array array_filter ( array $input [, callback $callback] );
Definition and Usage
PHP Notes By Deepak Mitra
17

Iterates over each value in the input array passing them to the callback function. If
the callback function returns true, the current value from input is returned into the result
array. Array keys are preserved.
Parameters
Parameter Description
input The array to iterate over
callback The callback function to use
If no callback is supplied, all entries of input equal to FALSE will be removed.
Return Values
Returns the filtered array.
Example
Try out following example:
<?php
function odd($var)
{
return($var & 1);
}

function even($var)
{
return(!($var & 1));
}

$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
$array2 = array(6, 7, 8, 9, 10, 11, 12);

echo "Odd :\n";
print_r(array_filter($array1, "odd"));
echo "Even:\n";
print_r(array_filter($array2, "even"));
?>
This will produce following result:
Odd :
Array ( [a] => 1 [c] => 3 [e] => 5)
Even:
Array ( [0] => 6 [2] => 8 [4] => 10 [6] => 12)
PHP Notes By Deepak Mitra
18

array_flip()
Syntax
array array_flip ( array $input );
Definition and Usage
array_flip() returns an array in flip order, i.e. keys from input become values and values
from input become keys.
If a value has several occurrences, the latest key will be used as its values, and all others
will be lost.
Paramters
Parameter Description
input The array to be fliped
Return Values
Returns FALSE if it fails otherwise fliped array.
Example
Try out following example:
<?php
$array = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);

print_r(array_flip($array));
?>
This will produce following result:
Array ( [1] => a [2] => b [3] => c [4] => d [5] => e)
array_intersect()
Syntax
array array_intersect ( array $array1, array $array2 [, array $array3 ...] );
Definition and Usage
Returns an array containing all the values of array1 that are present in all the arguments.
Note that keys are preserved.
PHP Notes By Deepak Mitra
19

Parameters
Parameter Description
array1 Required. The first array is the array that the others will be compared with.
array2 Required. An array to be compared with the first array
array3 Optional. An array to be compared with the first array
Return Values
Returns an array containing all the entries from array1 that are not present in any of the
other arrays.
Example
Try out following example:
<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
?>
This will produce following result:
Array ( [a] => green [0] => red)
array_intersect_assoc()
Syntax
array array_intersect_assoc ( array $array1, array $array2 [, array $array3 ...] );
Definition and Usage
Returns an array containing all the values of array1 that are present in all the arguments.
Note that the keys are used in the comparison unlike in array_intersect().
Parameters
Parameter Description
array1 Required. The first array is the array that the others will be compared with.
array2 Required. An array to be compared with the first array
array3 Optional. An array to be compared with the first array
PHP Notes By Deepak Mitra
20

Return Values
Returns an array containing all the values of array1 that are present in all the arguments.
Example
Try out following example:
<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("a" => "green", "yellow", "red");
$result = array_intersect_assoc($array1, $array2);
print_r($result);
?>
This will produce following result:
Array ( [a] => green )
ray to be compared with the first
array
array3
Optional. An array to be compared with the first
array
Return Values
Returns FALSE if there is any error.
Example
Try out following example:
<?php
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3 );
$array2 = array('green' => 4, 'blue' => 5, 'yellow' => 6,);

$result = array_intersect_key($array1, $array2);
print_r($result);
?>
This will produce following result:
Array ( [blue]=> 1 [green] => 3 )
array_intersect_uassoc()
Syntax
PHP Notes By Deepak Mitra
21

array_intersect_uassoc($array1, $array2 [, $array3 ..., callback $key_compare_func] );
Definition and Usage
Returns an array containing all the values of array1 that are present in all the arguments.
Note that the keys are used in the comparison unlike in array_intersect().
Parameters
Parameter Description
array1
Required. The first array is the array that the others will be compared
with.
array2 Required. An array to be compared with the first array
array3 Optional. An array to be compared with the first array
key_compare_func Required. User defined call back function.
Return Values
Returns an array containing all the values of array1 that are present in all the arguments.
Example
Try out following example:
<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");
$result = array_intersect_uassoc($array1, $array2, "strcasecmp");
print_r($result);
?>
This will produce following result:
Array ( [b] => brown )
array_intersect_ukey()
Syntax
array_intersect_ukey ( $array1, $array2 [, $array3..., callback $key_compare_func] );
Definition and Usage
Returns an array containing all the values of array1 which have matching keys that are
present in all the arguments.
PHP Notes By Deepak Mitra
22

Parameters
Parameter Description
array1 Required. The first array is the array that the others will be compared with.
array2 Required. An array to be compared with the first array
array3 Optional. An array to be compared with the first array
key_compare_func Required. User defined call back function.
Return Values
Returns FALSE if there is any error.
Example
Try out following example:
<?php
function key_compare_func($key1, $key2)
{
if ($key1 == $key2)
return 0;
else if ($key1 > $key2)
return 1;
else
return -1;
}
$array1 = array('blue'=>1, 'red'=>2, 'green'=>3, 'purple'=>4);
$array2 = array('green'=>5, 'blue'=>6, 'yellow'=>7, 'cyan'=>8);

$result = array_intersect_ukey($array1, $array2, "key_compare_func");
var_dump($result);
?>
This will produce following result:
array(2) {
["blue"]=>
int(1)
["green"]=>
int(3)
}
array_multisort()
Syntax
PHP Notes By Deepak Mitra
23

array_multisort(array1,sorting order,sorting type,array2...);
Definition and Usage
This can be used to sort several arrays at once, or a multi-dimensional array by one or more
dimensions.
Parameters
Parameter Description
array1 Required. Specifies an array
Sort order Optional. Specifies the sorting order. Possible values:
SORT_ASC Default. Sort in ascending order (A-Z)
SORT_DESC sort in descending order (Z-A)
Sorting type Optional. Specifies the type to use, when comparing elements. Possible
values:
SORT_REGULAR Default. Compare elements normally
SORT_NUMERIC Compare elements as numeric values
SORT_STRING Compare elements as string values
array2 Optional. Specifies an array
Return Values
Returns TRUE on success or FALSE on failure.
Example
Try out following example:
<?php
$array1 = array("10", 100, 100, "a");
$array2 = array(1, 3, "2", 1);
array_multisort($array1, $array2);
print_r($array1);
print_r($array2);
?>
This will produce following result:
Array
(
[0] => 10
PHP Notes By Deepak Mitra
24

[1] => a
[2] => 100
[3] => 100
)
Array
(
[0] => 1
[1] => 1
[2] => 2
[3] => 3
)
array_merge_recursive()
Syntax
array array_merge_recursive ( array $array1 [, array $array2...] )
Definition and Usage
Merges the elements of one or more arrays together so that the values of one are appended
to the end of the previous one.
If the input arrays have the same string keys, then the values for these keys are merged
together into an array, and this is done recursively, so that if one of the values is an array
itself, the function will merge it with a corresponding entry in another array too.
Parameters
Parameter Description
array1 Required.Specifies an array.
array2 Optional.Specifies an array.
Return Values
It returns the resulting array.
Example
Try out following example:
<?php
$array1=array("a"=>"Horse","b"=>"Cat","c"=>"Dog");
$array2=array("d"=>"Cow","a"=>"Cat","e"=>"elephant");

print_r(array_merge_recursive($array1,$array2));
?>
PHP Notes By Deepak Mitra
25

array_merge()
Syntax
array array_merge ( array $array1 [, array $array2 [, array $array3...]] );
Definition and Usage
Merges the elements of one or more arrays together so that the values of one are appended
to the end of the previous one.
If the input arrays have the same string keys, then the later value for that key will overwrite
the previous one.
Parameters
Parameter Description
array1 Required.Specifies an array.
array2 Optional.Specifies an array.
array3 Optional.Specifies an array.
Return Values
It returns the resulting array.
Example
Try out following example:
<?php
$array1=array("a"=>"Horse","b"=>"Cat","c"=>"Dog");
$array2=array("d"=>"Cow","a"=>"Cat","e"=>"elephant");

print_r(array_merge($array1,$array2));
?>
This will produce following result:
Array ( [a]=>Cat [b]=>Cat [c]=>Dog [d]=
array_map()
Syntax
array array_map ( callback $callback, array $array1 [, array $array2...] );
PHP Notes By Deepak Mitra
26

Definition and Usage
Returns an array containing all the elements of array1 after applying the callback function to
each one. The number of parameters that the callback function accepts should match the
number of arrays passed to the array_map().
Parameters
Parameter Description
callback Required. The name of the user-made function, or null.
array1 Required. Specifies an array.
array2 Optional. Specifies an array.
array3 Optional. Specifies an array.
Return Values
Returns an array containing all the processed elements of array1.
Example
Try out following example:
<?php
function cube($n)
{
return($n * $n * $n);
}

$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);
?>
array_keys()
Syntax
array_keys ( $input [, $search_value [, $strict]] );
Definition and Usage
Returns the keys, numeric and string, from the input array. If the optional search_value is
specified, then only the keys for that value are returned. Otherwise, all the keys from the
input are returned.
Parameters
PHP Notes By Deepak Mitra
27

Parameter Description
input Required.Specifies an array.
search_value
Required. You can specify a value, then only the keys with this value are
returned.
strict
Optional. Used with the value parameter. Possible values:
* true - Returns the keys with the specified value, depending on type: the
number 5 is not the same as the string "5".
* false - Default value. Not depending on type, the number 5 is the same as
the string "5".
Return Values
Returns the keys, numeric and string, from the input array
Example
Try out following example:
<?php
$a=array("a"=>"Horse","b"=>"Cat","c"=>"Dog");
print_r(array_keys($a));

$a=array("a"=>"Horse","b"=>"Cat","c"=>"Dog");
print_r(array_keys($a,"Dog"));

$a=array(10,20,30,"10");
print_r(array_keys($a,"10",false));
?>
This will produce following result:
Array ( [0] => a [1] => b [2] => c )
Array ( [0] => c)
Array ( [0] => 0 [1] => 3 )
array_key_exists()
Syntax
bool array_key_exists ( $key, $array );
Definition and Usage
PHP Notes By Deepak Mitra
28

Returns TRUE if the given key is set in the array. key can be any value possible for an
array index.
Parameters
Parameter Description
key Required. Key to be searched.
array Required. Array to be searched
Return Values
Returns TRUE if the given key is set in the array otherwise FALSE.
Example
Try out following example:
<?php
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
echo "The 'first' element is in the array";
}
?>
This will produce following result:
The 'first' element is in the array
array_pad()
Syntax
array_pad ( $array, $pad_size, $pad_value );
Definition and Usage
Returns a copy of the array padded to size specified by pad_size with value pad_value.
If pad_size is positive then the array is padded on the right, if it's negative then on the left.
If the absolute value of pad_size is less than or equal to the length of the input then no
padding takes place. It is possible to add most 1048576 elements at a time.
Parameters
Parameter Description
array Required.Specifies an array.
PHP Notes By Deepak Mitra
29

pad_size
Required.Specifies the number of elements in the array returned from the
function.
pad_value
Required.Specifies the value of the new elements in the array returned from
the function.
Return Values
It returns the resulting array.
Example
Try out following example:
<?php
$array1=array("a"=>"Horse","b"=>"Cat","c"=>"Dog");

print_r(array_pad($array1,7, "COW"));
?>
This will produce following result:
Array
(
[a] => Horse
[b] => Cat
[c] => Dog
[0] => COW
[1] => COW
[2] => COW
[3] => COW
)
array_pop()
Syntax
array_pop ( $array );
Definition and Usage
This function pops and returns the last value of the array, shortening the array by one
element. If array is empty (or is not an array), NULL will be returned.
Parameters
Parameter Description
array Required. Specifies an array.
PHP Notes By Deepak Mitra
30

Return Values
It returns the last value of the array, shortening the array by one element.
Example
Try out following example:
<?php
$array=array("a"=>"banana","b"=>"apple","c"=>"orange");

print_r(array_pop($array));
print_r("<br />");
print_r(array_pop($array));
?>
This will produce following result:
orange
apple
array_product()
Syntax
array_product ( $array );
Definition and Usage
It returns the product of values in an array as an integer or float. That is it multiplies all the
values and return final result.
Parameters
Parameter Description
array Required. Specifies an array.
Return Values
Product of values in an array.
Example
Try out following example:
<?php
$array=array(5,6,3);
PHP Notes By Deepak Mitra
31


print_r(array_product($array));
?>
This will produce following result:
180
array_push()
Syntax
array_push ( $array, $var1 [, $var2...] );
Definition and Usage
This function treats array as a stack, and pushes the passed variables var1, var2... onto
the end of array. The length of array increases by the number of variables pushed.
Parameters
Parameter Description
array Required. Specifies an array.
var1 Required. value to be pushed.
var2 Optional. value to be pushed.
Return Values
Returns the new number of elements in the array.
Example
Try out following example:
<?php
$array=array("a"=>"banana","b"=>"apple","c"=>"orange");

print_r(array_push($array, "mango"));
print_r("<br />");
print_r($array );
?>
This will produce following result:
4
PHP Notes By Deepak Mitra
32

Array ( [a] => banana [b] => apple [c] => orange [0] => mango )
array_rand()
Syntax
array_rand ( $input [, $num_req] );
Definition and Usage
This function pick one or more random entries out of an array.
Parameters
Parameter Description
array Required. Specifies an array.
num_req
OPtional. Specifies how many entries you want to pick - if not specified, it
defaults to 1.
Return Values
If you are picking only one entry, array_rand() returns the key for a random entry.
Otherwise, it returns an array of keys for the random entries.
Example
Try out following example:
<?php
$array=array("a"=>"banana","b"=>"apple","c"=>"orange");

print_r(array_rand($array));
?>
This will produce following result, This result will vary every time you will execute script:
b
array_reduce()
Syntax
array_reduce ( $array, callback $function [, int $initial] );
Definition and Usage
PHP Notes By Deepak Mitra
33

This function applies iteratively the function function to the elements of the array , so as
to reduce the array to a single value. If the optional initial is available, it will be used at the
beginning of the process, or as a final result in case the array is empty. If the array is
empty and initial is not passed.
Parameters
Parameter Description
array Required. Specifies an array.
function Required. Callback function.
initial Optional. Specifies the initial value to send to the function.
Return Values
Returns a reduced array.
Example
Try out following example:
<?php
function call_back_function($v1,$v2)
{
return $v1 . "-" . $v2;
}
$array=array("a"=>"banana","b"=>"apple","c"=>"orange");

print_r(array_reduce($array));
print_r("<br />");
print_r(array_reduce($array, 10));
?>
This will produce following result:
-banana-apple-orange
10-banana-apple-orange
array_reverse()
Syntax
array_reverse ( $array [, $preserve_keys] );
Definition and Usage
This function reverse the order of all the elements of a padded array.
PHP Notes By Deepak Mitra
34

Parameters
Parameter Description
array Required. Specifies an array.
preserve_keys
Optional. Specifies if order of keys also has to be changed ofr not. By
default its FALSE.
Return Values
Return an array with elements in reverse order.
Example
Try out following example:
<?php
$array=array("a"=>"banana","b"=>"apple","c"=>"orange");

print_r(array_reverse($array));
?>
This will produce following result:
Array ( [c] => orange [b] => apple [a] => banana )
array_search()
Syntax
array_search($value, $array [,$strict]);
Definition and Usage
The array_search() function search an array for a value and returns the key.
If value is a string, the comparison is done in a case-sensitive manner.
Parameters
Parameter Description
value Required. Specifies a value to be searched.
array Required. Specifies an array.
strict
Optional. If it is set to TRUE then the array_search() will also check the types
of the search in the array.
PHP Notes By Deepak Mitra
35

Return Values
Returns the key if it is found in the array, FALSE otherwise.
Example
Try out following example:
<?php
$array=array("a"=>"banana","b"=>"apple","c"=>"orange");

print_r(array_search("apple", $array));
?>
This will produce following result:
b
array_shift()
Syntax
array_shift ( $array );
Definition and Usage
This function shifts the first value of the array off and returns it, shortening the array by one
element and moving everything down. All numerical array keys will be modified to start
counting from zero while literal keys won't be touched.
Parameters
Parameter Description
array Required. Specifies an array.
Return Values
Returns first element of the array and if array is empty (or is not an array), NULL will be
returned.
Example
Try out following example:
<?php
$array=array("a"=>"banana","b"=>"apple","c"=>"orange");
PHP Notes By Deepak Mitra
36


print_r(array_shift($array));
print_r("<br />");
print_r(array_shift($array));
?>
This will produce following result:
banana
apple
array_slice()
Syntax
array_slice($array, $offset [,$length [,$preserve_keys]] );
Definition and Usage
This function returns the sequence of elements from the array array as specified by
the offset and length parameters.
If offset is non-negative, the sequence will start at that offset in the array. If offset is
negative, the sequence will start that far from the end of the array.
If length is given and is positive, then the sequence will have that many elements in it. If
length is given and is negative then the sequence will stop that many elements from the
end of the array. If it is omitted, then the sequence will have everything from offset up until
the end of the array.
Parameters
Parameter Description
array Required. Specifies an array.
offset Required. Numeric value. Specifies where the function will start the slice.
lenght Optional. Numeric value. Specifies the length of the slice.
preserve_keys
Optional. TRUE to preserve keys and FALSE to reset keys. Default is
FALASE.
Return Values
Returns the sequence of elements.
Example
Try out following example:
PHP Notes By Deepak Mitra
37

<?php
$input = array("a", "b", "c", "d", "e");
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
?>
This will produce following result:
Array
(
[0] => c
[1] => d
)
Array
(
[2] => c
[3] => d
)
array_splice()
Syntax
array_splice ( $input, $offset [,$length [,$replacement]] );
Definition and Usage
This function removes the elements designated by offset and length from
the input array, and replaces them with the elements of the replacement array, if
supplied. It returns an array containing the extracted elements.
Parameters
Parameter Description
input Required. Specifies an array
offset Required. Numeric value. Specifies where the function will start removing
elements. 0 = the first element. If this value is set to a negative number, the
function will start that far from the last element. -2 means start at the second
last element of the array.
length Optional. Numeric value. Specifies how many elements will be removed, and
also length of the returned array. If this value is set to a negative number, the
function will stop that far from the last element. If this value is not set, the
function will remove all elements, starting from the position set by the start-
parameter.
replacement Optional. Specifies an array with the elements that will be insertet to the
original array. If it's only one element, it can be a string, and does not have to
PHP Notes By Deepak Mitra
38

be an array.
Return Values
It returns the last value of the array, shortening the array by one element.
Example
Try out following example:
<?php
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
print_r($input);
print_r("<br />

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
print_r($input);
print_r("<br />

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
print_r($input);
print_r("<br />

$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
print_r($input);
print_r("<br />

$input = array("red", "green", "blue", "yellow");
array_splice($input, 3, 0, "purple");
print_r($input);
print_r("<br />

?>
This will produce following result:
Array ( [0]=>red [1] =>green )
Array ( [0]=>red [1] =>yellow )
Array ( [0]=>red [1] =>orange )
Array ( [0]=>red [1] =>green [2]=>blue [3]=>black [4]=>maroon )
Array ( [0]=>red [1] =>green [2]=>blue [3]=>purple [4]=>yellow )
array_sum()
Syntax
PHP Notes By Deepak Mitra
39

array_sum ( $array );
Definition and Usage
Calculate the sum of values in an array and returns the sum of values in an array as an
integer or float.
Parameters
Parameter Description
array Required. Specifies an array.
Return Values
It returns the sum of values in an array as an integer or float.
Example
Try out following example:
<?php
$a = array(2, 4, 6, 8);
echo "sum(a) = " . array_sum($a) . "<br />";

$b = array("a" => 1.2, "b" => 2.3, "c" => 3.4);
echo "sum(b) = " . array_sum($b) . "<br />";
?>
This will produce following result:
sum(a) = 20
sum(b) = 6.9
array_udiff()
Syntax
array_udiff( $array1, $array2 [, $array3 ..., $data_compare_func] );
Definition and Usage
Computes the difference of arrays by using a callback function for data comparison and
returns an array containing all the values from array1 that are not present in any of the
other arguments.
Parameters
PHP Notes By Deepak Mitra
40

Parameter Description
array1 Required. Specifies an array.
array2 Required. Specifies an array to be compared with the first array.
array3 Optional. Specifies an array to be compared with the first array.
data_compare_func Required. The name of the user-made function.
Return Values
Returns an array containing all the values from array1 that are not present in any of the
other arguments.
Example
Try out following example:
<?php
function call_back_function($v1,$v2)
{
if ($v1===$v2)
{
return 0;
}
return 1;
}
$array1 = array("a"=>"orange","b"=>"mango","c"=>"banana");
$array2 = array("a"=>"orange","b"=>"mango","c"=>"apple");
print_r(array_udiff_assoc($array1,$array2,"call_bak_function"));
?>
This will produce following result:
Array ( [c]=>banana
array_udiff_assoc()
Syntax
array_udiff_assoc ( $array1, $array2 [, $array3 ..., $data_compare_func] );
Definition and Usage
Computes the difference of arrays with additional index check, compares data by a callback
function and returns an array containing all the values from array1 that are not present in
any of the other arguments.
Parameters
PHP Notes By Deepak Mitra
41

Parameter Description
array1 Required. Specifies an array.
array2 Required. Specifies an array to be compared with the first array.
array3 Optional. Specifies an array to be compared with the first array.
data_compare_func Required. The name of the user-made function.
Return Values
Returns an array containing all the values from array1 that are not present in any of the
other arguments.
Example
Try out following example:
<?php
function call_back_function($v1,$v2)
{
if ($v1===$v2)
{
return 0;
}
return 1;
}
$array1 = array("a"=>"orange","b"=>"apple","c"=>"mango");
$array2 = array("a"=>"orange","b"=>"mango","c"=>"apple");
print_r(array_udiff_assoc($array1,$array2,"call_bak_function"));
?>
This will produce following result:
Array ( [b]=>apple [c] => mango )
array_udiff_uassoc()
Syntax
array_udiff_uassoc ( $array1, $array2 [, $array3 ..., $func1, $func2] );
Definition and Usage
The array_udiff_uassoc() function compares two or more arrays, in two user-made
functions, and returns an array containing the elements from the first array, if the user-
made functions allow it. The first user-made function compares array keys, and the second
compares array values, and both returns a numeric value, a positive number (1) if the
returned array should contain this element, and 0, or a negative number (-1), if not.
PHP Notes By Deepak Mitra
42

Parameters
Parameter Description
array1 Required. Specifies an array.
array2 Required. Specifies an array to be compared with the first array.
array3 Optional. Specifies an array to be compared with the first array.
func1 Required. The name of the user-made function that compares the array keys.
func2 Required. The name of the user-made function that compares the array values.
Return Values
Returns an array containing all the values from array1 that are not present in any of the
other arguments.
Example
Try out following example:
<?php
function func1($v1,$v2)
{
if ($v1===$v2)
{
return 0;
}
return 1;
}
function func2($v1,$v2)
{
if ($v1===$v2)
{
return 0;
}
return 1;
}
$array1 = array("a"=>"orange","b"=>"mango","c"=>"banana");
$array2 = array("a"=>"orange","b"=>"mango","c"=>"apple");
print_r(array_udiff_uassoc($array1,$array2,"func1", "func2"));
?>
This will produce following result:
Array ( [c]=>banana )
array_uintersect()
PHP Notes By Deepak Mitra
43

Syntax
array_uintersect ( $array1, $array2 [, $array3 ..., $data_compare_func] );
Definition and Usage
This function returns an array containing all the values of array1 that are present in all the
arguments. The data is compared by using a callback function.
Parameters
Parameter Description
array1 Required. Specifies an array.
array2 Required. Specifies an array to be compared with the first array.
array3 Optional. Specifies an array to be compared with the first array.
data_compare_func Required. The name of the user-made function.
Example
Try out following example:
<?php
$array1 = array("a"=>"green", "b"=>"brown", "c"=>"blue", "red");
$array2 = array("a"=>"GREEN", "B"=>"brown", "yellow", "red");

print_r(array_uintersect($array1, $array2, "strcasecmp"));
?>
This will produce following result:
Array
(
[a] => green
[b] => brown
[0] => red
)
array_uintersect_assoc()
Syntax
array_uintersect_assoc( $array1, $array2 [, $array3 ..., $data_compare_func] );
Definition and Usage
PHP Notes By Deepak Mitra
44

This function returns an array containing all the values of array1 that are present in all the
arguments array2, array3.
Parameters
Parameter Description
array1 Required. Specifies an array.
array2 Required. Specifies an array to be compared with the first array.
array3 Optional. Specifies an array to be compared with the first array.
data_compare_func Required. The name of the user-made function.
Example
Try out following example:
<?php
$array1 = array("a"=>"green", "b"=>"brown", "c"=>"blue", "red");
$array2 = array("a"=>"GREEN", "B"=>"brown", "yellow", "red");

print_r(array_uintersect_assoc($array1, $array2, "strcasecmp"));
?>
This will produce following result:
Array
(
[a] => green
)
array_uintersect_uassoc()
Syntax
array_uintersect_assoc( $array1, $array2 [, $array3 ..., $func1], $func2 );
Definition and Usage
This function returns an array containing all the values of array1 that are present in all the
arguments array2, array3.
Parameters
Parameter Description
array1 Required. Specifies an array.
array2 Required. Specifies an array to be compared with the first array.
PHP Notes By Deepak Mitra
45

array3 Optional. Specifies an array to be compared with the first array.
func1 Required. The name of the user-made function that compares the array keys.
func2 Required. The name of the user-made function that compares the array values.
Example
Try out following example:
<?php
$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"));
?>
This will produce following result:
Array
(
[a] => green
[b] => brown
)
array_unique()
Syntax
array_unique ( $array );
Definition and Usage
The array_unique() function removes duplicate values from an array. If two or more array
values are the same, the first appearance will be kept and the other will be removed.
Parameters
Parameter Description
array1 Required. Specifies an array.
Example
Try out following example:
<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
PHP Notes By Deepak Mitra
46

$result = array_unique($input);
print_r($result);
?>
This will produce following result:
Array
(
[a] => green
[0] => red
[1] => blue
)
array_unshift()
Syntax
array_unshift($array,$value1,$value2,$value3...)
Definition and Usage
This function returns an array containing all the values of array1 that are present in all the
arguments array2, array3.
Parameters
Parameter Description
array Required. Specifies an array.
value1 Required. Specifies a value to insert
value2 Optional. Specifies a value to insert
value3 Optional. Specifies a value to insert
Example
Try out following example:
<?php
$queue = array("orange", "banana");
array_unshift($queue, "apple", "raspberry");
print_r($queue);
?>
This will produce following result:
Array
PHP Notes By Deepak Mitra
47

(
[0] => apple
[1] => raspberry
[2] => orange
[3] => banana
)
array_values()
Syntax
array_values ( $array );
Definition and Usage
This function returns all the values from the input array and indexes numerically the array.
Parameters
Parameter Description
array Required. Specifies an array.
Example
Try out following example:
<?php
$array = array("a"=>"green", "b"=>"brown", "c"=>"blue", "red");

print_r(array_values($array));
?>
This will produce following result:
Array
(
[0] => green
[1] => brown
[2] => blue
[3] => red
)
array_walk()
Syntax
array_walk ( $array, $funcname [, $parameter] );
PHP Notes By Deepak Mitra
48

Definition and Usage
This function returns an array containing all the values of array1 that are present in all the
arguments array2, array3.
Parameters
Parameter Description
array Required. Specifies an array.
funcname Required. The name of the user-made function.
paramter Optional. Specifies a parameter to the user-made function.
Example
Try out following example:
<?php
function call_back_function($value,$key)
{
echo "The key $key has the value $value<br />";
}

$array = array("a"=>"green", "b"=>"brown", "c"=>"blue", "red");

array_walk($array,"call_back_function");
?>
This will produce following result:
The key a has the value green
The key b has the value brown
The key c has the value blue
The key 0 has the value red
array_walk_recursive()
Syntax
array_walk_recursive( $array, $funcname [,$parameter])
Definition and Usage
The array_walk_recursive() function runs each array element in a user-made function. The
array's keys and values are parameters in the function. The difference between this function
and the array_walk() function is that with this function you can work with deeper arrays (an
array inside an array). Returns True or False.
PHP Notes By Deepak Mitra
49

Parameters
Parameter Description
array Required. Specifies an array.
funcname Required. The name of the user-made function.
paramter Optional. Specifies a parameter to the user-made function.
Example
Try out following example:
<?php
function call_back_function($value,$key)
{
echo "The key $key has the value $value<br />";
}

$array1 = array("a"=>"green", "b"=>"brown", "c"=>"blue" );
$array2 = array($array1, "d"=>"yellow", "e"=>"black");

array_walk_recursive($array,"call_back_function");
?>
This will produce following result:
The key a has the value green
The key b has the value brown
The key c has the value blue
The key d has the value yellow
The key e has the value black
arsort()
Syntax
arsort( $array [, $sort_flags] );
Definition and Usage
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.
Parameters
Parameter Description
PHP Notes By Deepak Mitra
50

array Required. Specifies an array.
sort_flags
Optional. Specifies how to sort the array values. Possible values:
SORT_REGULAR - Default. Treat values as they are (don't change
types)
SORT_NUMERIC - Treat values numerically
SORT_STRING - Treat values as strings
SORT_LOCALE_STRING - Treat values as strings, based on local
settings
Return Value
Returns TRUE on success or FALSE on failure.
Example
Try out following example:
<?php
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana" );
arsort($fruits);
print_r($fruits);
?>
This will produce following result:
Array ( [a] => orange [d] => lemon [b] => banana )
asort()
Syntax
asort( $array [, $sort_flags] );
Definition and Usage
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.
Parameters
Parameter Description
array Required. Specifies an array.
sort_flags Optional. Specifies how to sort the array values. Possible values:
PHP Notes By Deepak Mitra
51

SORT_REGULAR - Default. Treat values as they are (don't change
types)
SORT_NUMERIC - Treat values numerically
SORT_STRING - Treat values as strings
SORT_LOCALE_STRING - Treat values as strings, based on local
settings
Return Value
Returns TRUE on success or FALSE on failure.
Example
Try out following example:
<?php
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana" );
asort($fruits);
print_r($fruits);
?>
This will produce following result:
Array ( [b] => banana [d] => lemon [a] => orange )
compact($var1, $var2...);
Definition and Usage
This function takes a variable number of parameters. Each parameter can be either a string
containing the name of the variable, or an array of variable names. The array can contain
other arrays of variable names inside it; compact() handles it recursively.
Parameters
Parameter Description
var1 Required. Can be a string with the variable name, or an array of variables.
var2 Optional. Can be a string with the variable name, or an array of variables.
Return Value
It returns the output array with all the variables added to it.
Example
Try out following example:
PHP Notes By Deepak Mitra
52

<?php
$city = "San Francisco";
$state = "CA";
$event = "SIGGRAPH";

$result = compact("city", "state", "event");
print_r($result);
?>
This will produce following result:
Array
(
[city] => San Francisco
[state] => CA
[event] => SIGGRAPH
)
count()
Syntax
count($array, $mode );
Definition and Usage
Count elements in an array, or properties in an object.
If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count() will recursively
count the array. This is particularly useful for counting all the elements of a
multidimensional array. The default value for mode is 0. count() does not detect infinite
recursion.
Parameters
Parameter Description
array Required. Specifies an array
mode Optional. Specifies the mode of the function.
Return Value
Returns the number of elements in an array.
Example
Try out following example:
PHP Notes By Deepak Mitra
53

<?php
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
$result = count($a);
print($result);
?>
This will produce following result:
3
current()
Syntax
current ( $array );
Definition and Usage
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 value of the array element that's currently being
pointed to 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.
Parameters
Parameter Description
array Required. Specifies an array
Return Value
Returns the current element in an array.
Example
Try out following example:
<?php
$transport = array('foot', 'bike', 'car', 'plane');
$mode = current($transport);
print "$mode <br />";
$mode = next($transport);
print "$mode <br />";
$mode = current($transport)
PHP Notes By Deepak Mitra
54

print "$mode <br />";;
$mode = prev($transport);
print "$mode <br />";
$mode = end($transport);
print "$mode <br />";
$mode = current($transport);
print "$mode <br />";
?>
This will produce following result:
foot
bike
bike
foot
plane
plane

You might also like