What’s the difference between sort(), assort() and ksort?
Under what circumstances would you use each of these?




Answers were Sorted based on User's Feedback



What’s the difference between sort(), assort() and ksort? Under what circumstances would you use ..

Answer / deep

1) sort()
This function sorts an array. Elements will be arranged
from lowest to highest when this function has completed.

<?php

$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
echo "fruits[" . $key . "] = " . $val . "\n";
}

?>
----------------------------OUTPUT---------------------
fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange
-------------------------------------------------------

2) asort()
This function sorts an array such that array indices
maintain their correlation with the array elements they are
associated with. This is used mainly when sorting
associative arrays where the actual element order is
significant.

<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" =>
"banana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>

--------------------OUTPUT------------------------
c = apple
b = banana
d = lemon
a = orange
--------------------------------------------------

3) ksort()
Sorts an array by key, maintaining key to data
correlations. This is useful mainly for associative arrays.

<?php
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana",
"c"=>"apple");
ksort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>

--------------------OUTPUT----------------------------
a = orange
b = banana
c = apple
d = lemon
------------------------------------------------------

Is This Answer Correct ?    27 Yes 0 No

What’s the difference between sort(), assort() and ksort? Under what circumstances would you use ..

Answer / hardik

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

<?php

$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
echo "fruits[" . $key . "] = " . $val . "\n";
}

?>
----------------------------OUTPUT---------------------
fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange
-------------------------------------------------------

2) asort()
This function sorts an array such that array indices
maintain their correlation with the array elements they are
associated with. This is used mainly when sorting
associative arrays where the actual element order is
significant.

<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" =>
"banana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>

--------------------OUTPUT------------------------
c = apple
b = banana
d = lemon
a = orange
--------------------------------------------------

3) ksort()
Sorts an array by key, maintaining key to data
correlations. This is useful mainly for associative arrays.

<?php
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana",
"c"=>"apple");
ksort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>

--------------------OUTPUT----------------------------
a = orange
b = banana
c = apple
d = lemon
------------------------------------------------------

Is This Answer Correct ?    4 Yes 0 No

Post New Answer




More PHP Interview Questions

How can I find what type of images that the PHP version supports?

1 Answers  


List out some tools through which we can draw E-R diagrams for mysql?

6 Answers  


What is the full form of PHP?

27 Answers   Apple, PHP, Nandhini,


am from coimbatore.am learning php&mysql. what will be the approx salary for me if i get a job in coimbatore.

0 Answers   HCL,


How do you run a php script from the command line?

1 Answers  






explain in brief type juggling in php?

1 Answers  


Why using the htaccess file

2 Answers  


i'm b.com graduate and doing MCM- 1year(Master in Computer Management. i'm very interested in PHP? Please tell me some suggetion and the scope of PHP in the market. My E-mail pawan.register@gmail.com Thanks & Regards Pawan.

1 Answers  


Give any ten basic functions in PHP?

4 Answers   T3 Softwares,


What is the use of paypal in the e-commerece website?

3 Answers   RedAlkemi,


How can we do user authentication without using session/cookies ? For ex:- From page1 accepts user name and password and need to check in all other pages whether the user has logged in or not

6 Answers   TCS,


Is the cookies are available on cross domain?

2 Answers  






Categories