PHP sort() function

This function is used to sort the array elements in ascending order.

Syntax

sort(array, sort_flags);

The array is the required parameter. sort_flags is optional parameter, it is used to set the sorting behavior. Like - SORT_REGULAR is used to compare elements normally, SORT_STRING is used to compare elements as strings.

Example 1

<?php
    $arr = array(10, 11, 35, 12, 44);
    sort($arr);
    print_r($arr);
?>

Output of the above code -

Array ( [0] => 10 [1] => 11 [2] => 12 [3] => 35 [4] => 44 )

Example 2

<?php
    $arr = array("car", "bus", "train", "cycle");
    sort($arr);
    print_r($arr);
?>

Output of the above code -

Array ( [0] => bus [1] => car [2] => cycle [3] => train )

Example 3

<?php
    $arr = array(10, 11, 35, 12, 44);
    sort($arr, SORT_NUMERIC);
    print_r($arr);
?>

Output of the above code -

Array ( [0] => 10 [1] => 11 [2] => 12 [3] => 35 [4] => 44 )



Related PHP Functions

PHP array_reverse() function
PHP array_diff() function
PHP array_key_exists() function
PHP array_push() function
PHP array_search() function
PHP class_exists() function
PHP curl_setopt() function
PHP die() function
PHP dirname() function
PHP each() function
PHP explode() function
PHP file_exists() function
PHP function_exists() function
PHP getenv() function
PHP is_readable() function
PHP ksort() function
PHP mkdir() function
PHP ob_start() function
PHP parse_url() function
PHP str_repeat() function
PHP substr() function




Read more articles


General Knowledge



Learn Popular Language