Friday, April 9, 2010

asort arsort diffrerences

Question: what is the difference between asort and arsort?

Answer:

asort - sorts an array. It maintains index values.

arsort- It sorts an array in reverse manner.

Example for asort:


<?php
$fruits = array("d" => "php", "a" => "interview", "b" => "questions", "c" => "answers");
asort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>

The output is c = answers a = interview d = php b = questions

Example for arsort:

<?php
$fruits = array("d" => "php", "a" => "interview", "b" => "questions", "c" => "answers");
arsort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>

The output is

b = questions d = php a = interview c = answers

No comments:

Post a Comment