PHP sort() function

P
PHP sizeof() function
PHP uasort() function

In this article, you will learn how to sort an indexed array in ascending order. The sort() function arranges the elements of an indexed array in ascending order.

What is the syntax of the sort() function in PHP?

sort(array, sorttype)
ParameterDetails
arrayThe array to sort in ascending order – Required
sorttypeOptional. Specifies how to compare the array elements/items. 
SORT_STRING (default) – Compare elements as strings
SORT_REGULAR – Compares normally without changing the type
SORT_NUMERIC – Compare as numeric values
SORT_LOCALE_STRING – Compare as a string according to the current locale
PHP sort() function

Examples of sort() function

Example 1. Sort the elements of the array in ascending order alphabetically.

<?php
$arr=array("Suzuki","Corolla","Honda");
sort($arr);
?>

Example 2. Sort the elements of the array in ascending order numerically.

<?php
$n=array(5,7,4,6,23,32);
sort($n);
?>
PHP sizeof() function
PHP uasort() function

1 comment

Tutor Network

Share this

Learn PHP from A to Z