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

What is member variable?

0 Answers  


What are the differences between public, private, protected, static, transient, final and volatile?

3 Answers  


Is laravel frontend or backend?

0 Answers  


What is string in php?

0 Answers  


Does php use html?

0 Answers  






hi,am a fresher in sap sd,will i try as a fresher or i wl go for with 2 to 3 yr exp,pls suggest me which web site is best for sap freshers.

0 Answers  


what is php stands for?

23 Answers   Infosys, Satyam, Torque Infotech,


what server connected

1 Answers   Wipro,


How to send a cookie to the browser?

0 Answers  


I created a new joomla module for administrator. when am going to install this, it is going "joomla/modules" path. but, i want to install this in the "joomla/administration/modules" path.

2 Answers  


Do you know what is the function mysql_pconnect() usefull for?

0 Answers  


Tell me what does accessing a class via :: means?

0 Answers  


Categories