Difference Between Sort and Asort in PHP

In this tutorial i will tell you difference between sort() and asort() php function.Basicly this is very common question for intrview preperation.

The function sort() and asort() functions are used to sort the array in php.
asort are use to sorts an array by the values but keep their original keys whereas sort doen’t keep their original keys.

sort(): function syntax – sort(array,sorttype)
Parameters :
sorttype - is Optional. Specifies how to sort the array values.
SORT_REGULAR - Default. Treat values as they are (dont change types)
SORT_NUMERIC - Treat values numerically
SORT_STRING - Treat values as strings
SORT_LOCALE_STRING - Treat values as strings, based on local settings

This function assigns new keys for the elements in the array. Existing keys will be deleted.

eg. See the code below when we create array $myarray we have assigned keys as a b & c.
Now we are applying sort function and printing the array.

view plainprint?
$my_array = array("a" => “chaprak”, “b” => “programming”, “c” => “dexter”);

print_r($my_array);
echo ”
“;
sort($my_array);
echo ”
“;
print_r($my_array);
echo ”
“;
?>

so the output of the above code will be -
initial array contents –

Array ( [a] => chaprak [b] => programming [c] => dexter )

and after applying sort function –

Array ( [0] => chaprak [1] => dexter [2] => programming )

its keys are changed to numeric values and sorting is done.

View php manual for the array sort function

* asort() function syntax – asort(array,sorttype)

The asort() function sorts an array by the values. The values keep their original keys (index).

sorttype is Optional. Specifies how to sort the array values.

* SORT_REGULAR – Default. Treat values as they are (don’t change types)
* SORT_NUMERIC – Treat values numerically
* SORT_STRING – Treat values as strings
* SORT_LOCALE_STRING – Treat values as strings, based on local settings

view plainprint?
$my_array = array("a" => “chaprak”, “b” => “programming”, “c” => “dexter”);
echo ”
“;
print_r($my_array);
echo ”
“;
asort($my_array);
echo ”
“;
print_r($my_array);
?>

when we execute above code then it will produce the following output

initially array contents are – Array ( [a] => chaprak [b] => programming [c] => dexter )

after applying asort() function the contents are –

Array ( [a] => chaprak [c] => dexter [b] => programming )

see here sorting is done and even index is maintained.

View php manual for the array sort function

The following two tabs change content below.
Hi, This is Parvez Alam from India. I am software developer with 4 years’ experience in web development. I have submitted articles on PHP, Mysql, Magento,CSS, HTML, jQuery, web designing and social API. You can subscribe to my blog via RSS/Twitter/Google plus and Facebook. parvez1487(at)gmail(dot)com

One thought on “Difference Between Sort and Asort in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>