Array Functions in PHP


Array functions allow you to manipulate arrays.
Using array function you can manipulate one dimensional as well as multi dimensional array.
Following are various array manipulation functions with their syntax and example.

(1) count:

count () function accepts an array as an argument and returns number of elements available in an array.
Syntax:
count (Array Name)

<<php
$MyArray = array ("A","B","C");
echo count($MyArray);
?>
Output:
3


     

(2) list:

list () function accepts one or more variables as an argument and assign them values from the array.
Thus you can assign values to more then one variables using single line of code.
Syntax:
List (Variable1, Variable2… VariableN) = ArrayName;

<?php
$MyArray = array("Pankaj","Hiren","Amit");
list($Name1,$Name2,$Name3)=$MyArray;
echo $Name1."<br/>";
echo $Name2."<br/>";
echo $Name3."<br/>";
?>
Output:
Pankaj
Hiren
Amit


(3) in_array:

in_array () function is used to search specific value from the array.
It returns Boolean value. If value found in the array then it returns true otherwise it returns false value. If the SearchValue is of type string then it performs case sensitive search.
Syntax:
in_array (SearchValue, ArrayName, SearchType)
Here,
SearchValue
is the value that you want to search in the array.
ArrayName is the name of array in which you want to search value.
SearchType indicates which type of search you want to perform. It is optional argument. By default it is set to FALSE. If you set its value to TRUE then it will perform data type specific search.

<?php
$MyArray = array("Pankaj","Hiren","Amit");
if (in_array("pankaj",$MyArray))
{
echo "String Found in Array <br/>";
}
else
{
echo "String Not Found in Array <br/>";
}
$MyArray1 = array(11,12,13);
if (in_array("11",$MyArray1,true))
{
echo "String Found in Array <br/>";
}
else
{
echo "String Not Found in Array <br/>";
}
$MyArray2 = array(11,12,13);
if (in_array("11",$MyArray2,false))
{
echo "String Found in Array <br/>";
}
else
{
echo "String Not Found in Array <br/>";
}
?>
Output:
String Not Found in Array
String Not Found in Array
String Found in Array


(4) current:

current () function accepts array as an argument and returns the value of the current element in an array. By default first element is the current element of an array.
Syntax:
current (ArrayName)

<?php
$MyArray = array ("Pankaj","Hiren","Amit");
echo current($MyArray);
?>
Output:
Pankaj


(5) next:

next() function accepts array as an argument and moves its internal pointer one position ahead so that it points to the next element in the array from the current position.
It returns Boolean value. Calling this function when internal pointer is on the last element returns FASLE value otherwise it returns true value.
Syntax:
next (ArrayName)

<?php
$MyArray = array("Pankaj","Hiren","Amit");
echo current($MyArray)."<br/>";
next($MyArray);
echo current($MyArray)."<br/>";
?>
Output:
Pankaj
Hiren


(6) prev:

prev () function accepts array as an argument and moves its internal pointer one position down so that it points to the previous element in the array from the current position.
It returns Boolean value. Calling this function when internal pointer is on the first element returns FASLE value otherwise it returns true value.
Syntax:
prev (ArrayName)

<?php
$MyArray = array("Pankaj","Hiren","Amit");
echo current($MyArray)."<br/>";
next($MyArray);
echo current($MyArray)."<br/>";
prev ($MyArray);
echo current($MyArray)."<br/>";
?>
Output:
Pankaj
Hiren
Pankaj


(7) end:

end () function accepts array as an argument and moves its internal pointer to last position so that it points to the last element in the array.
Syntax:
end (ArrayName)

<?php
$MyArray = array("Pankaj","Hiren","Amit");
echo current($MyArray)."<br/>";
end($MyArray);
echo current($MyArray)."<br/>";
?>
Output:
Pankaj
Amit


(8) each:

each () function accepts an array as an argument and returns the key and value of current element and then it will moves the internal pointer forward to the next element.
This function returns a Boolean value. It returns true if element available in the array list otherwise it returns false.
Syntax:
each (ArrayName)

<?php
$Batsman = array("Sachin", "Sehwag", "Gambhir");
print_r (each($Batsman));
print_r (each($Batsman));
print_r (each($Batsman));
?>
Output:
Array ( [1] => Sachin [value] => Sachin [0] => 0 [key] => 0 )
Array ( [1] => Sehwag [value] => Sehwag [0] => 1 [key] => 1 )
Array ( [1] => Gambhir [value] => Gambhir [0] => 2 [key] => 2 )


(9) sort:

sort () function accepts an array as an argument and sorts elements of an array in ascending order.
This function performs sorting based on the values not on key.
It returns Boolean value. If elements sorted successfully then it returns true otherwise it returns false.
Syntax:
array_sort (ArrayName, SortType)
Here,
ArrayName
indicates name of array to be sorted.
SortType indicates type of sorting you want to perform. It is optional argument. You can specify one of the following values to it:
(1) SORT_REGULAR
(2) SORT_NUMERIC
(3) SORT_STRING
(4) SORT_LOCALE_STRING

<?php
$Batsman = array("Sachin", "Sehwag", "Gambhir");
sort($Batsman);
print_r($Batsman);
?>
Output:
Array ( [0] => Gambhir [1] => Sachin [2] => Sehwag )


(10) array_merge:

array_merge () function accepts one or more then one array as an argument merge them and returns a single array that contains all the elements.
Syntax:
array_merge (ArrayName1, ArrayName2, ArrayNameN)

<?php
$Batsman = array("Sachin", "Sehwag", "Gambhir");
$Bowler = array("Harbhajan", "Zahir", "nehra");
$Team = array_merge($Batsman, $Bowler);
print_r($Team);
?>
Output:
Array ( [0] => Sachin [1] => Sehwag [2] => Gambhir [3] => Harbhajan [4] => Zahir [5] => nehra )


(11) array_reverse:

array_reverse () function accepts an array as an argument and returns an array that contains elements in the reverse order of the specified array.
It accepts one optional argument named preserve. It can have Boolean value. It specifies weather you want to preserve the key of the array element or not. By default it is set to false. Means it can not preserve the key of the array element.
Syntax:
array_reverse (ArrayName, Preserve)

<? php
$Name = array ("Sachin", "Sehwag", "Gambhir");
print_r($Name);
$Name1 = array_reverse($Name);
print_r($Name1);
$Name2 = array_reverse($Name,true);
print_r($Name2);
?>
Output:
Array ( [0] => Sachin [1] => Sehwag [2] => Gambhir )
Array ( [0] => Gambhir [1] => Sehwag [2] => Sachin )
Array ( [2] => Gambhir [1] => Sehwag [0] => Sachin )

Download Projects


Download Programs