Array Functions Examples in PHP

Array Functions Examples in PHP - An array is a way of storing a lot of data in one variable. Usually in other programming languages, the data stored on the array must be similar data. However, in PHP you are free to store data with any type eg string and integer. Then, the data stored in these variables can be distinguished by using the index. PHP provides more than 70 functions for array manipulation. The array functions in PHP can be found at http://ca.php.net/manual/en/ref.array.php.
Array Sorting Function
- arsort() - Sorting array by values descending
- asort() - Ordering of arrays based on values ascending
- krsort () - Sorting arrays by index / key in descending order
- ksort() - Sorting array based on index / keys ascending
- rsort() - Sorting arrays by values descending with change index / key
- sort() - Sorting arrays by values ascending with change index / key
- shuffle() - Random sorting array
Pointer Array Setting Function
- current() - Obtain an array element designated by a pointer
- end() - The pointer points to the last array element
- key() - Obtain the key pointed by the pointer
-next() - The pointer points to the next element
- prev() - The pointer points to the previous element
- reset() - Move the array pointer to the beginning (first element)
- count() - Calculates the number of array elements
Search Functions on Arrays
- array_search() - Finds the position (key) of a value in the array
- array_key_exists() - Checks a key in the array or not
- in_array () - Checks an element in the array or not
Here is a program, example of array functions in php
Program 1
File Name: array06.php
Description: Programs sort the array by sort() and rsort().
Open notepad, then type the following php code.
<?php
$arrValue = array ("Johans" => 80, "Otim" => 90, "Jessica" => 75,
"Michael" => 85);
echo "<b>Array before sorting</b>";
echo "<pre>";
print_r($arrValue);
echo "</pre>";
sort($arrValue);
reset($arrValue);
echo "<b>Array after sorting with sort()</b>";
echo "<pre>";
print_r($arrValue);
echo "</pre>";
rsort($arrValue);
reset($arrValue);
echo "<b>Arrays after sorting with rsorts()</b>";
echo "<pre>";
print_r($arrValue);
echo "</pre>";
?>

Array Functions Examples in PHP

Save the code in the htdocs folder named array06.php. To run the program open the browser then type in the address bar http://localhost/array06.php then enter. The result will look like the following.
Array Functions Examples in PHP
Program view

Program 2
File Name: array07.php
Description: Programs sort the array with asort() and arsort().
<?php
$arrValue = array ("Johans" => 80, "Otim" => 90, "Jessica" => 75,
"Michael" => 85);
echo "<b>Array before sorting</b>";
echo "<pre>";
print_r($arrValue);
echo "</pre>";
asort($arrValue);
reset($arrValue);
echo "<b>Array after sorting with asort()</b>";
echo "<pre>";
print_r($arrValue);
echo "</pre>";
arsort($arrValue);
reset($arrValue);
echo "<b>Array after sorting with arsort()</b>";
echo "<pre>";
print_r($arrValue);
echo "</pre>";
?>

Array Functions Examples in PHP

Array Functions Examples in PHP
Program view

Program 3
File Name: array08.php
Description: Programs sort the array with ksort() and krsort()
<?php
$arrValue = array ("Johans" => 80, "Otim" => 90, "Jessica" => 75,
"Michael" => 85);
echo "<b>Array before sorting</b>";
echo "<pre>";
print_r($arrValue);
echo "</pre>";
ksort($arrValue);
reset($arrValue);
echo "<b>Array after sorting with ksort()</b>";
echo "<pre>";
print_r($arrValue);
echo "</pre>";
krsort($arrValue);
reset($arrValue);
echo "<b>Array after sorting with krsort()</b>";
echo "<pre>";
print_r($arrValue);
echo "</pre>";
?>

Array Functions Examples in PHP

Array Functions Examples in PHP
Program view

Program 4
File Name: array09.php
Description: The program set the position of the pointer in the array.
<?php
$transport = array('foot', 'bike', 'car', 'plane');
echo "<pre>";
print_r ($transport);
echo "</pre>";
$mode = current($transport);
echo $mode."<br>"; // $mode = 'foot';
$mode = next($transport);
echo $mode."<br>"; // $mode = 'bike';
$mode = current($transport);
echo $mode."<br>"; // $mode = 'bike';
$mode = prev($transport);
echo $mode."<br>"; // $mode = 'foot';
$mode = end($transport);
echo $mode."<br>"; // $mode = 'plane';
$mode = current($transport);
echo $mode."<br>"; // $mode = 'plane';
?>

Array Functions Examples in PHP

Array Functions Examples in PHP
Program view

Program 5
File Name: array10.php
Description: The program looks for array elements.
<?php
$arrFruit = array ("Mango", "Apple", "Banana", "Watermelon",
"Orange");
if (in_array ("Watermelon", $arrFruit)) {
    echo "There is a Watermelon fruit here"; 
} else {
    echo "There is no Watermelon fruit here"; 
}
?>

Array Functions Examples in PHP

Array Functions Examples in PHP
Program view

That's the explanation we can give you about Array Functions Examples in PHP, hopefully can improve your knowledge about php programming language. Learn also articles about Declaration and Displaying Array in PHP Example.

Comments