«

»

Mar 27

useful function in php

Sort

This function use for sort an array. Element will arrange lowest to highest.

Syntax

bool Sort($array, sortingtype or flag)

Sortingtype : Optional, specify how the compare element value
1. SORT_REGULAR : Default. Compare items normally.
2. SORT_NUMERIC : Compare items numerically
3. SORT_STRING : Compare items as strings
4. SORT_STRING : Compare items as strings
5. SORT_LOCALE_STRING : Compare items as strings, based on current locale
6. SORT_NATURAL : Compare items as strings using natural ordering
7. SORT_FLAG_CASE : can be combined with SORT_STRING or SORT_NATURAL to sort strings case-insensitively

Return Value
TRUE on Success and FALSE on Failure

Example 1
$arrval=array(“pen”, “Pencil”, “Book”, “Class Room”);
Sort($arrval);
print_r($arrval);

Output
array(
[0] => Book
[1] => Class Room
[2] => pen
[3] => Pencil
)

Example 2

$arrval = array(“Orange1”, “orange2”, “Orange3”, “orange20”);
sort($arrval, SORT_NATURAL | SORT_FLAG_CASE);
Print_r($arrval);

Output
Array (
[0] => Orange1
[1] => orange2
[2] => Orange3
[3] => orange20
)

Rsort

This function use for Sort an array in reverse order.

Syntax :
rsort(array,sortingtype);

sortingtype is same as sort function.

Return value

TRUE on success and FALSE on failure

Example :

$arrvar=array(“Volvo”,”BMW”,”Toyota”);
rsort($arrvar,SORT_NUMERIC);
Print_r($arrvar);

Output:
Array(
[0] => Volvo
[1] => Toyota
[2] => BMW
)

Asort

Sort an array and maintain index association

Syntax:
asort(array,sortingtype)

Sortingtype : sortingtype is optional parameter sort_flag

Return Value : true on success and false on failure

Example:

$arrval=array("d"=>"test5", "g"=>"test3", "f"=>"test2", "c"=>"test1","a"=>"test6", "b"=>"test4")
asort($arrval)
Print_r($arrval);

Output:
array(
“c”=>”test1”
“f”=>”test2”
“g”=>”test3”
“b”=>”test4”
“d”=>”test5”
“a”=>”test6”
)

Arsort

Sort an array in reverse order and maintain index association

Syntax:
arsot(array,sortingtype)
sortingtype:sortingtype is optional parameter sort_flag

Return Value : true on success and false on failure

Example:

$arrval=array("d"=>"test5", "g"=>"test3", "f"=>"test2", "c"=>"test1","a"=>"test6", "b"=>"test4")
arsort($arrval)
Print_r($arrval);

Output:
array(
“a”=”test6”
“d”=>”test5”
“b”=>”test4”
“g”=>”test3”
“f”=>”test2”
“c”=>”test1”
)

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>