What is difference between ksort() and usort() functions.

devquora
devquora

Posted On: Feb 22, 2018

 

  • ksort() function is used to sort an array according to its key values whereas asort() function is used to sort an array according to its values.
  • They both used to sort an associative array in PHP.

Example of asort():

<?php
$age = array("Peter"=>"37", "Ben"=>"27", "Joe"=>"43");
asort($age);
?>

Output: Key=Ben, Value=37 Key=Joe, Value=43 Key=Peter, Value=35

Example of ksort():

<?php
$age = array("Peter"=>"37", "Ben"=>"27", "Joe"=>"43");
ksort($age);
?>

Output: Key=Ben, Value=37

Key=Joe, Value=43
Key=Peter, Value=35

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    PHP Interview Questions

    What is T_PAAMAYIM_NEKUDOTAYIM in PHP?

    T_PAAMAYIM_NEKUDOTAYIM is scope resolution operator used as :: (double colon) .Basically, it used to call static methods/variables of a Class...

    PHP Interview Questions

    What is the difference between == and === operator in PHP ?

    In PHP == is equal operator and returns TRUE if $a is equal to $b after type juggling and === is Identical operator and return TRUE if $a is equal to $b, and they are of the same data type...