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 ?    28 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

Is it possible to connect to a Microsoft Access database without a DSN? If so, how??

3 Answers  


I have two radio button, i click one radio button display one dropdown box, one text field. and then click another radio button display one text field.so any one give me good idea plz.....i used <div id="" style=display:none> but not work..

4 Answers  


Do you know what is the difference between mysql_fetch_object() and mysql_fetch_array()?

0 Answers  


What is the difference between for and foreach?

0 Answers  


Make a "dynamic drop down list" with using only PHP,HTML and MySQL..

2 Answers  






What is polymorphism in oop php?

0 Answers  


Which function is used to strip whitespace?

0 Answers  


How be the result set of mysql handled in php?

0 Answers  


Is php a case sensitive language?

0 Answers  


Is overloading possible in php?

0 Answers  


How many types of inheritances used in php and how we achieve it.

0 Answers  


What types images that php supports?

1 Answers  


Categories