PHP - Function sort()


Advertisements


Syntax

sort( $array [, $sort_flags] );

Definition and Usage

This function sorts an array. Elements will be arranged from lowest to highest when this function has completed.

Parameters

Sr.No Parameter & Description
1

array(Required)

It specifies an array.

2

sort_flags(Optional)

It specifies how to sort the array values. Possible 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

Return Value

It returns TRUE on success or FALSE on failure.

Example

Try out following example −

<?php
   $input = array("d"=>"lemon", "a"=>"orange", "b"=>"banana" );
   sort($input);
   print_r($input);
?> 

This will produce the following result −

Array (
   [0] => banana
   [1] => lemon
   [2] => orange
)

php_function_reference.htm

Advertisements