PHP Array Functions
PHP Array Functions allow you to access and manipulate arrays. Simple and multi-dimensional arrays are supported.
Some Important Functions of PHP Arrays are listed below:
array()
Returns an array of the parameters. The parameters can be given an index with the => operator
Syntax
array(key1 => value1, key2 => value2..
.)
Parameters
- 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
<?php
$b = array("a"=>"horse", "b"=>"aunt", "c"=>"dog");
print_r($b);
?>
Run : http://ideone.com/7bpwAw
Array
(
[a] => horse
[b] => aunt
[c] => dog
)
sizeof ()
This function is used to count the items of the array. (count() is also used instead of this).
<?php
$cars=array("Volvo","BMW","Toyota");
echo sizeof($cars);
?>
Run : http://ideone.com/2wkVF6
3
array_merge()
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one.
Syntax
array array_merge ( array $array1 [, array $array2 [, array $array3...]] );
Parameters
- array1-Required. Specifies an array.
- array2-Optional. Specifies an array.
- array3-Optional. Specifies an array.
<?php
$array1 = array("a"=>"Red","b"=>"Blue","c"=>"Pink");
$array2 = array("d"=>"Green","a"=>"Brown","e"=>"Saphoron");
print_r(array_merge($array1,$array2));
?>
Run : http://ideone.com/Q1tfcO
Array
(
[a] => Brown
[b] => Blue
[c] => Pink
[d] => Green
[e] => Saphoron
)
count()
Count elements in an array, or properties in an object.
Syntax
count($array, $mode );
Parameters
- array-Required. Specifies an array
- mode-Optional. Specifies the mode of the function.
<?php
$a[0] = 10;
$a[1] = 35;
$a[2] = 50;
$No_count = count($a);
print($No_count);
?>
Run : http://ideone.com/TVH8PK
3
array_chunk()
Chunks an array into size large chunks. The last chunk may contain less than size elements.
Syntax
array array_chunk ( array $input, int $size [, bool $preserve_keys] );
Parameters
- 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
<?php
$array1 = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($array1, 2));
print_r(array_chunk($array1, 2, true));
?>
Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[0] => c
[1] => d
)
[2] => Array
(
[0] => e
)
)
Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[2] => c
[3] => d
)
[2] => Array
(
[4] => e
)
)
array_unique
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.
Syntax
array_unique ( $array );
Parameters
- array1-Required. Specifies an array.
<?php
$color = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($color);
print_r($color);
?>
Run : http://ideone.com/bCzu4S
Array
(
[a] => green
[0] => red
[b] => green
[1] => blue
[2] => red
)
Sort Functions For Arrays
sort()
sort arrays in ascending order
<?php
$cars = array("Swift", "Honda City", "Elentra");
sort($cars);
print_r($cars);
?>
Run : http://ideone.com/epYYuF
Array
(
[0] => Elentra
[1] => Honda City
[2] => Swift
)
rsort()
sort arrays in descending order
<?php
$cars = array("Swift", "Honda City", "Elentra");
rsort($cars);
print_r($cars);
?>
Run : http://ideone.com/XIUr2f
Array
(
[0] => Swift
[1] => Honda City
[2] => Elentra
)
asort()
sort associative arrays in ascending order, according to the value
<?php
$age = array("john"=>"35", "smith"=>"37", "Joe"=>"43");
asort($age);
print_r($age);
?>
Run : http://ideone.com/EThDYb
Array
(
[john] => 35
[smith] => 37
[Joe] => 43
)
ksort()
sort associative arrays in ascending order, according to the key
<?php
$age = array("john"=>"35", "smith"=>"37", "Joe"=>"43");
ksort($age);
print_r($age);
?>
Run : http://ideone.com/W47oLf
Array
(
[Joe] => 43
[john] => 35
[smith] => 37
)
arsort()
sort associative arrays in descending order, according to the value
<?php
$age = array("john"=>"35", "smith"=>"37", "Joe"=>"43");
arsort($age);
print_r($age);
?>
Run : http://ideone.com/9YtqRL
Array
(
[Joe] => 43
[smith] => 37
[john] => 35
)
krsort()
sort associative arrays in descending order, according to the key
<?php
$age = array("john"=>"35", "smith"=>"37", "Joe"=>"43");
krsort($age);
print_r($age);
?>
Run : http://ideone.com/g6bsJZ
Array
(
[smith] => 37
[john] => 35
[Joe] => 43
)
Exercises & Assignments |
---|
No Content Found. |
Interview Questions & Answers |
---|
No Content Found. |