1.count
: The function returns the number of elements in an array.
Syntax:
int count(array);
<?php
$cars=array("Volvo","BMW","Toyota");
echo count($cars); //3
?>
<?php
$cars=array("Volvo","BMW","Toyota");
echo count($cars); //3
?>
2. list : The function
is used to assign values
to a list of variables
in one operation.
Syntax: list(var1,var2...)
$my_array = array("Dog","Cat","Horse");
list($a, $b, $c) = $my_array;
echo "I have several animals, a $a, a $b and a $c.";
?>
Output: I have several animals, a Dog, a Cat and a Horse.
3. In_array
: The in_array() function
searches an array for
a specific value. This function
returns TRUE if the value
is found in the array, or FALSE otherwise.
<?php
$cars=array("Volvo","BMW","Toyota");
echo current($cars) . "<br />"; //Volvo
echo next($cars)."<br />"; //BMW
echo prev($cars)."<br />";//Volvo
echo end($cars) . "<br />";//Toyota
?>
Syntax:
in_array(search,array)
<?php
$fruit = array("mango",
"apple", "banana", "grapes");
if
(in_array("grapes",$fruit))
{
echo "Match found"; //match
found
}
else
{
echo "Match not found";
}
?>
4.
current
: The current() function returns the value of the current element in an array.
Syntax: current(array)
5. next : The
next() function moves
the internal pointer to, and outputs, the next element in the array.
Syntax: next(array)
6.
prev: The
prev() function moves the internal
pointer to, and outputs,
the previous element in the array.
Syntax: prev(array)
7. end: The
end() function moves
the internal pointer
to, and outputs, the last element in the array.
<?php
$cars=array("Volvo","BMW","Toyota");
echo current($cars) . "<br />"; //Volvo
echo next($cars)."<br />"; //BMW
echo prev($cars)."<br />";//Volvo
echo end($cars) . "<br />";//Toyota
?>
8. each : The each() function returns the
current element key and value, and moves the internal pointer forward. This element key an value is
returned in an array with four elements. Two elements (1 and Value) for the
element value, and two elements
(0 and Key) for the element key.
$cars=array("Volvo","BMW","Toyota");
print_r (each($people));
?>
Output: Array ( [1] => Volvo [value] => Volvo [0] => 0 [key] => 0 )
Syntax: array each(array);
<?php$cars=array("Volvo","BMW","Toyota");
print_r (each($people));
?>
Output: Array ( [1] => Volvo [value] => Volvo [0] => 0 [key] => 0 )
9. sort
: The sort() function sorts an indexed array in ascending order.
Syntax:
sort(array);
10. rsort:
The rsort() function sorts an indexed array in descending orde
Syntax: sort(array);
11. asort : The asort() function sorts an associative array in ascending
order, according to the value.
Syntax:asort(array);
12.
arsort : The asort() function sorts an associative array in ascending order, according to the value.
Syntax:arsort(array);
<?php$cars=array("Volvo","BMW","Toyota");
sort($cars);
print_r($cars);//Array ( [0] => BMW [1] => Toyota [2] => Volvo )
rsort($cars);
print_r($cars);// Array ( [0] => Volvo [1] => Toyota [2] => BMW )
asort($cars);
print_r($cars);// Array ( [1] => BMW [2] => Toyota [0] => Volvo )
arsort($cars);
print_r($cars);// Array ( [0] => Volvo [2] => Toyota [1] => BMW )
?>
13.
array_merge:
The array_merge() function merges one or more arrays into one array.
Syntax: array_merge(array1,array2,array3...)
<?php$cars=array("Volvo","BMW","Toyota");
$byk=array(“pulser”,”r15”,”karizma”);
$a= array_merge($cars,$byk); Print_r($a);
?>
Output: Array ( [0] => Volvo [1] => BMW [2] => Toyota [3] => pulser [4] => r15 [5] => karizma )
14.
array_reverse:
The array_reverse() function returns an array in the reverseorder.
Syntax: array_reverse(array)
<?php
$cars=array("Volvo","BMW","Toyota");
print_r(array_reverse($cars)); Array ( [0] => Toyota [1] => BMW [2]
=> Volvo )
?>
15. array_diff: This function compares the values of two (or more) arrays,
and return an array
that contains time.<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_diff($a1,$a2);
print_r($result);//Array ( [d] => yellow )
?>.
Syntax: array_diff(array1,array2,array3...);
16. array_merge_recursive: it merges
the elements of one or more arrays
together so that the values of one are appended to the end of the previous
one. It returns the resulting array.
<?php$a1 = array('first'=>'bob', 'last'=>'jones', 'age'=>'48');
$a2 = array('first'=>'sam', 'last'=>'smith', 'age'=>'41');
$a3 = array('first'=>'pete', 'last'=>null, 'age'=>'3');
$a4 = array('first'=>'joe', 'last'=>'johnson', 'age'=>'33');
$a5 = array_merge_recursive($a1,$a2,$a3,$a4); print_r($a5);
?>
Output:
Array (
[first] => Array (
[0] => bob
[1] => sam
[2] => pete
[3] => joe
)
[last] => Array (
[0] => jones
[1] => smith [2] =>
[3] => johnson
)
[age] => Array (
[0] => 48
[1] => 41
[2] => 3
[3] => 33
)
)
Syntax: array array_merge_recursive (array1,array2,array3...);
17. array_shift: The array_shift() function removes
the first element
from an array, and returns
the value of the
removed element.
$cars=array("Volvo","BMW","Toyota"); array_shift($cars);
print_r($cars); //Array ( [0] => BMW [1] => Toyota );
?>
$a=array("a"=>"Cat","b"=>"Dog","c"=>"Cat");
print_r(array_unique($a)); // Array ( [a] => Cat [b] => Dog )
?>
$a=array("a"=>"Cat","b"=>"Dog");
array_unshift($a,"Horse");
print_r($a); // Array ( [0] => Horse [a] => Cat [b] => Dog ) array_push ($a,"sparrow","Bird");
print_r($a); //Array ( [0] => Horse [a] => Cat [b] => Dog [1] => sparrow [2] => Bird )
array_pop ($a);
print_r($a); //Array ( [0] => Horse [a] => Cat [b] => Dog [1] => sparrow )
?>
$a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
print_r(array_keys($a));// Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 )
?>
$a=array("a"=>"Dog","b"=>"Cat");
if (array_key_exists("a",$a))
echo "Key exists!"; //key exists!
else
echo "Key does not exist!";
?>
Syntax:
array_shift(array)
<?php$cars=array("Volvo","BMW","Toyota"); array_shift($cars);
print_r($cars); //Array ( [0] => BMW [1] => Toyota );
?>
18. array_slice:
The array_slice() function returns selected parts of an array.
Syntax: array_slice(array,start)
<?php
$a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
print_r(array_slice($a,1)); //Array ( [0] => Cat [1] => Horse [2] => Bird )
?>
$a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
print_r(array_slice($a,1)); //Array ( [0] => Cat [1] => Horse [2] => Bird )
?>
19. 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)
<?php$a=array("a"=>"Cat","b"=>"Dog","c"=>"Cat");
print_r(array_unique($a)); // Array ( [a] => Cat [b] => Dog )
?>
20. array_unshift: The array_unshift() function
inserts new elements
to an array. The new array values
will be inserted in the
beginning of the array
Syntax: array_unshift(array,value1,value2,value3...)
21.
array_push:
The array_push() function inserts one or more elements to the end of an array.
Syntax: array_push(array,value1,value2...)
22. array_pop:
The array_pop() function deletes the last element of an array.
Syntax:
array_pop(array)
<?php$a=array("a"=>"Cat","b"=>"Dog");
array_unshift($a,"Horse");
print_r($a); // Array ( [0] => Horse [a] => Cat [b] => Dog ) array_push ($a,"sparrow","Bird");
print_r($a); //Array ( [0] => Horse [a] => Cat [b] => Dog [1] => sparrow [2] => Bird )
array_pop ($a);
print_r($a); //Array ( [0] => Horse [a] => Cat [b] => Dog [1] => sparrow )
?>
23. array_keys:
The array_keys() function returns an array containing the keys.
Syntax: array_keys(array)
<?php$a=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird");
print_r(array_keys($a));// Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 )
?>
24. array_key_exists: The
array_key_exists() function checks an array for a specified key, and returns
true if the key exists and false if the key does not exist.
Syntax:
array_key_exists(key,array)
<?php$a=array("a"=>"Dog","b"=>"Cat");
if (array_key_exists("a",$a))
echo "Key exists!"; //key exists!
else
echo "Key does not exist!";
?>
25. array_multisort: The array_multisort()
function returns a sorted array. You can assign one or more
arrays. The function sorts the first array, and
the other arrays follow, then, if two or more values are the same, it sorts the
next array, and so on.
Syntax: array_multisort(array1,
array2,array3...)
<?php
$a1=array("d","C");
$a2=array("b","h");
array_multisort($a1,$a2);
print_r($a1); //Array ( [0]
=> b [1] => d ) print_r($a2); //Array ( [0] => c [1] => d )
?>
26.array_search: The array_search() function search an array for a value and returns the key.
Syntax: array_search(value,array)
<?php
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
echo array_search("Dog",$a); //a
?>
26.array_search: The array_search() function search an array for a value and returns the key.
Syntax: array_search(value,array)
<?php
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse");
echo array_search("Dog",$a); //a
?>
No comments:
Post a Comment