PHP sort() function with example

PHP sort() function with example

PHP sort() work: Here, we will find out about the sort() work with the example in PHP?

PHP sort() work

sort() work is utilized to sort a recorded exhibit in rising request.

On the off chance that exhibit components are numeric, it sorts dependent on the numbers, if cluster components are the string, it sorts dependent on the letters in order and if the cluster contains numeric values and content/strings, it sorts components dependent on the letter sets.

It doesn’t restore an arranged cluster, it sorts the information exhibit.

Syntax:

    sort(array, [mode]);

Here,

  • cluster is an information exhibit
  • the mode is a discretionary parameter, its default value is 0, it has the following values:
    • 0 – It is utilized to look at things ordinarily
    • 1 – It is utilized to think about things numerically
    • 2 – It is utilized to analyze things as strings
    • 3 – It is utilized to look at things as present region strings
    • 4 – It is utilized to think about things as strings (normal requesting)

Example:

    Input:
    $arr1 = array(105, 108, 101, 100, 90);

    Output:
    arr1 (sorted)...
    Array
    (    
        [0] => 90   
        [1] => 100  
        [2] => 101  
        [3] => 105  
        [4] => 108  
    )

PHP Code:

<?php    
    $arr1 = array(105, 108, 101, 100, 90);
    $arr2 = array("Kishan", "Durgesh", "Aakash", "Manish", "Ashwin");
	
    sort($arr1);
    print ("arr1 (sorted)...\n");
    print_r ($arr1);

    sort($arr2);
    print ("arr2 (sorted)...\n");
    print_r ($arr2);    
?>

Output:

arr1 (sorted)...
Array
(    
    [0] => 90   
    [1] => 100  
    [2] => 101  
    [3] => 105  
    [4] => 108  
)    
arr2 (sorted)...
Array
(    
    [0] => Ashwin
    [1] => Manish
    [2] => Kishan
    [3] => Durgesh
    [4] => Aakash
)

Recent Posts

error: Alert: Content is protected!!