PHP Array Function
1- sort()

This function sort an array values in ascending order.

Note :- This function return true on success otherwise false on failure.

Return Type :- bool

<?php

$my_array=array("Delhi","Mumbai","Kolkata","Chennai");

echo "<b>Unsort array</b>"."<br>";
foreach($my_array as $i)
echo $i."<br>";

sort($my_array);

echo "<br>"."<b>Sort array</b>"."<br>";

foreach($my_array as $i)
echo $i."<br>";

?>

Output:-
Unsort array
Delhi
Mumbai
Kolkata
Chennai

Sort array
Chennai
Delhi
Kolkata
Mumbai


2- rsort()

This function sort an array values in descending order.

Note :- This function return true on success otherwise false on failure.

Return Type :- bool

<?php

$my_array=array("Delhi","Mumbai","Kolkata","Chennai");

echo "<b>Unsort array</b>"."<br>";
foreach($my_array as $i)
echo $i."<br>";

rsort($my_array);

echo "<br>"."<b>Sort array</b>"."<br>";

foreach($my_array as $i)
echo $i."<br>";

?>

Output :-
Unsort array
Delhi
Mumbai
Kolkata
Chennai

Sort array
Mumbai
Kolkata
Delhi
Chennai

3- ksort()

This function sort an array values by key value in ascending order.

Note :-This function bascially use for data corelation This function return true on success otherwise false on failure.

Return Type :- bool

In this example order bykey value four,one,three,two.

<?php
$my_array["one"]="Delhi";
$my_array["two"]="Chennai";
$my_array["three"]="Sun";
$my_array["four"]="Mumbai";

echo "<b>Unsort array</b>"."<br>";
foreach($my_array as $i)
echo $i."<br>";

ksort($my_array);

echo "<br>"."<b>Sort array</b>"."<br>";
foreach($my_array as $i)
echo $i."<br>";
?>


Output :-
Unsort array
Delhi
Chennai
Sun
Mumbai

Sort array
Mumbai
Delhi
Sun
Chennai
4- krsort()

This function sort an array values by key value in descending order.

Note :- This function bascially use for data corellation This function return true on success otherwise false on failure.

Return Type :- bool

<?php
$my_array["one"]="Delhi";
$my_array["two"]="Chennai";
$my_array["three"]="Surat";
$my_array["four"]="Mumbai";

echo "<b>Unsort array</b>"."<br>";
foreach($my_array as $i)
echo $i."<br>";

krsort($my_array);


echo "<br>"."<b>Sort array</b>"."<br>";
foreach($my_array as $i)
echo $i."<br>";
?>

Output :-
Unsort array
Delhi
Chennai
Surat
Mumbai

Sort array
Chennai
Surat
Delhi
Mumbai

5- asort()

This function sort an array value(ascending) and it also maintain index assocation.

Return Type :- bool

<?php
$my_array["one"]="Delhi";
$my_array["two"]="Chennai";
$my_array["three"]="Surat";
$my_array["four"]="Mumbai";

echo "<b>Unsort array</b>"."<br>";
foreach($my_array as $i)
echo $i."<br>";

asort($my_array);

echo "<br>"."<b>Sort array</b>"."<br>";
foreach($my_array as $i)
echo $i."<br>";
?>

Output :-
Unsort array
Delhi
Chennai
Surat
Mumbai

Sort array
Chennai
Delhi
Mumbai
Surat

6- arsort()

This function sort an array value(descending) and it also maintain index assocation.

Return Type :- bool

<?php
$my_array["one"]="Delhi";
$my_array["two"]="Chennai";
$my_array["three"]="Surat";
$my_array["four"]="Mumbai";

echo "<b>Unsort array</b>"."<br>";
foreach($my_array as $i)
echo $i."<br>";

arsort($my_array);


echo "<br>"."<b>Sort array</b>"."<br>";
foreach($my_array as $i)
echo $i."<br>";
?>

Output :-
Unsort array
Delhi
Chennai
Surat
Mumbai

Sort array
Surat
Mumbai
Delhi
Chennai

7- array_merge()

This function 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.

Return Type :- array

<?php

$array1 = array("Delhi","Chennai");

$array2 = array("Kolkata","Mumbai");

$result = array_merge($array1, $array2);

for($i=0;$i<4;$i++)
echo $result[$i]."<br>";

?>

Output :-
Delhi
Chennai
Kolkata
Mumbai

8- list()


Assign variables to array values.

Return Type :- void

<?php

$my_array = array("Delhi","Chennai","Kolkata","Mumbai");

list($a,$b,$c,$d)=$my_array;

echo $a;
echo "<br>";
echo $b;
echo "<br>";
echo $c;
echo "<br>";
echo $d;
echo "<br>";

?>

Output :-
Delhi
Chennai
Kolkata
Mumbai

Some Important Array function(Brief discussion)

array() :- This function create an array.
array_fill() :- This function fill array with values. Return type (array)
count() : This function count the number of elements in an array and properties of an object.
Return type (int)


Previous
Next