sort & ksort

sort & ksort

sort

sort
It sort an elements of a given array.

Syntax:

void sort ( array array [, int sort_flags])

- This function sorts an array. Elements will be arranged from lowest to highest when this function has completed.

ksort

ksort
It Sort an array by key.

Syntax:
int ksort ( array array [, int sort_flags])

It sorts an array by key, maintaining key to data correlations. This is useful mainly for associative arrays.

Example:

$input1 = array (”lemon”, “orange”, “banana”, “apple”);
$input2 = array (”d”=>”lemon”, “a”=>”orange”, “b”=>”banana”, “c”=>”apple”);

sort ($input1);
ksort ($input2);

print_r($input1);
print_r($input2);

The result in $input1 is
Array
(
[0] => apple
[1] => banana
[2] => lemon
[3] => orange
)

The result in $input2 is
Array
(
[a] => orange
[b] => banana
[c] => apple
[d] => lemon
)

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.