Difference between sort(), asort() and ksort in php
Posted by Raj
Difference between sort(), asort() and ksort in php
sort():
sorts an array by the values.
Syntax
sort(arr,type)
Example:
<?php
$arr = array("a" => "Blue", "b" => "White", "c" => "Orange");
sort($arr);
print_r($arr);
?>
Output:
Array
(
[0] => Blue
[1] => Orange
[2] => White
)
asort():
sorts an array by the values but keep their original keys.
Syntax
asort(arr,type)
Example:
<?php
$arr = array("a" => "Blue", "b" => "White", "c" => "Orange");
sort($arr);
print_r($arr);
?>
Output:
Array
(
[a] => Blue
[c] => Orange
[b] => White
)
ksort():
sorts an array by the keys.
Syntax
ksort(arr,type)
Example:
<?php
$arr = array("a" => "Blue", "b" => "White", "c" => "Orange");
sort($arr);
print_r($arr);
?>
Output:
Array
(
[a] => Blue
[c] => White
[b] => Orange
)
This entry was posted on October 4, 2009 at 12:14 pm, and is filed under
array sort,
asort(),
ksort(),
PHP,
PHP interview questions and answers,
sort array in php,
sort script,
sort()
.You can leave a response, or trackback from your own site.