PHP asort() function

PHP asort() function

The asort() function in PHP is used to sort an array while maintaining the index association.

Syntax

asort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) 

array(required)- This parameter represents the input array to sort.

sort_flags (optional)- This parameter is used to modify the behavior of the sort. The various possible values for sorting type flags are as follows:

  • SORT_REGULAR – It is used to compare items normally (don't change types)
  • SORT_NUMERIC – This flag is used to compare the items numerically.
  • SORT_STRING – It compares the given items as strings.
  • SORT_LOCALE_STRING – This flag compares the items as strings, based on the current locale.
  • SORT_NATURAL – It is used to compare items as strings using "natural ordering" like natsort().
  • SORT_FLAG_CASE – It can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort the given strings case-insensitively

Return

This function returns a Boolean value TRUE on success or FALSE on failure.

Example 1

"Reema", "Age"=>"23","Country"=>"India");
 //original array
 echo("Original array: \n");
 print_r($array);
 //sorting the array while maintaining the index association
 asort($array);
 //printing the sorted array 
 echo("\nSorted array: \n");
 print_r($array);
 ?> 

Output

Original array: 
 Array
 (
     [Name] => Reema
     [Age] => 23
     [Country] => India
 ) 
 Sorted array: 
 Array
 (
     [Age] => 23
     [Country] => India
     [Name] => Reema
 ) 

Example 2

"Reema", "Age"=>23,"Country"=>"India");
 //original array
 echo("Original array: \n");
 print_r($array);
 //sorting the array while maintaining the index association
 asort($array );
 //printing the sorted array 
 echo("\nSorted array without second parameter: \n");
 print_r($array);
 //sorting array with second parameter as SORT_STRING 
 asort($array,SORT_STRING  );
 //printing the sorted array
 echo("\nSorted array with second parameter: \n");
 print_r($array);
 ?> 

Output

Original array: 
 Array
 (
     [Name] => Reema
     [Age] => 23
     [Country] => India
 )
 Sorted array without second parameter:  
 Array
 (
     [Country] => India
     [Name] => Reema
     [Age] => 23
 )
 Sorted array with second parameter: 
 Array 
 (
     [Age] => 23
     [Country] => India
     [Name] => Reema
 ) 

Example 3

"mango", 2 => "orange", 3 => "lichi", "apple");
 //sorting the array
 asort($fruits);
 echo("Sorted Array: \n");
 //printing the sorted values
 foreach ($fruits as $key => $val) {
     echo "$key = $val\n";
 }
 ?> 

Output

Sorted Array: 
4 = apple
3 = lichi
1 = mango
2 = orange 

Related Topics

PHP array_udiff() Function

PHP array_udiff() Function The array_udiff() function in PHP calculates the difference of arrays by using a callback function for data comparison. Syntax array_udiff ( array $array1 , array $array2 [, array $... ], callable $value_compare_func ) Parameter array1(required)- This array represents the first input array. array2(required)- This array represents the second input array. array3….(optional)- It...

1 minute read.

PHP array_keys() Function

PHP array_keys() Function The array_keys() function  in PHP is used to return all the keys, numeric and string from the given array. Syntax array_keys ( array $array ) or array_keys ( array $array , mixed $search_value [, bool $strict = FALSE ] )  Parameter array(required)- This parameter specifies the input array search_value(optional)- This parameter specifies a value, where...

1 minute read.

PHP money_format() Function

PHP money_format() Function The money_format() function in PHP formats a number as a currency string and returns a formatted version of number. Syntax money_format ( string $format , float $number )  Parameter format(required)- This parameter specifies the string to be formatted and how to format the...

1 minute read.

PHP stripcslashes() Function

PHP stripcslashes() Function The stripcslashes() function in PHP returns a string with backslashes(\n, \r ..., octal and hexadecimal representation) stripped off. Syntax stripcslashes ( string $str )  Parameter str(required)- This parameter signifies the string to be unescaped. Return This function returns the unescaped string with backslashes stripped off. Example 1   ...

1 minute read.

PHP For Loops

PHP for loop is used when the specified condition is predefined. Syntax: for (initialization; condition; increment/decrement ) { code to be executed; } Example <!DOCTYPE html> <html> <head> <title>PHP Tutorial</title> </head> <body> <?php echo " Print counting using for loop"."<br>"; for($i=1;$i<=5;$i++){ echo $i."<br>"; } ?> </body> </html> Output Print counting using...

1 minute read.

PHP isset() function

PHP isset is used to check whether the variable is set or not.The PHP isset() function returns false if variable contains a NULL value. Syntax: isset(variable1, variable2….) Presentation of PHP isset: In...

2 minutes read.

PHP array_values() Function

PHP array_values() Function The array_values () function in PHP returns all the values from the array and indexes the array numerically. Syntax array_values ( array $array ) Parameter array(required)- This array represents the input array. Return This function returns an indexed array of values. Example 1 Output Input Array: Array ...

1 minute read.

PHP strcspn() Function

PHP strcspn() Function The strcspn() function in PHP returns the number of characters (including whitespaces) found in a string before any part of the specified characters is found. It is a binary-safe function. Syntax strcspn ( string $subject , string $mask [, int $start [, int $length ]] ) Parameter subject(required)- This...

1 minute read.

PHP strtr() Function

PHP strtr() Function The strtr() function in PHP translates or replaces certain characters in a string. Syntax strtr ( string $str , string $from , string $to ) or strtr ( string $str , array $replace_pairs ) Parameter str(required)- This parameter specifies the string to translate from(required)- This parameter represents the string being translated to the parameter ‘to’. to(required)- It specifies what...

1 minute read.

PHP str_pad() Function

PHP str_pad() Function The str_pad() function in PHP pads a string to the given length with another string. Syntax str_pad ( string $input , int $pad_length [, string $pad_string  [, int $pad_type ]] ) Parameter input(required)- This parameter signifies the input string. pad_length(required)- This parameter specifies the length of the new string. If this value...

2 minutes read.

PHP end() function

PHP end() function The end() function in PHP sets the internal pointer of an array to its last element. This function returns the value of the last element Syntax end ( array &$array )   Parameter array (required)– This parameter represents the input array. Return This function returns...

1 minute read.

PHP convert_uuencode() Function

PHP convert_uuencode() Function The convert_uuencode() function in PHP is used to encode a string and returns the uuencoded data. Syntax convert_uuencode ( string $data ) Parameter data(required)- This parameter represents the data to be encoded. Return This function returns the uuencoded data or FALSE on failure. Example 1 <?php //...

2 minutes read.

PHP natcasesort() Function

PHP natcasesort() Function The natcasesort() function in PHP Sort an array using a case insensitive "natural order" algorithm while maintaining the keys. Syntax natcasesort ( array &$array ) Parameter array(required)- This parameter represents the input array. Return This parameter returns a Boolean...

1 minute read.

PHP key_exists() Function

PHP key_exists() Function The key_exists() function in PHP checks if the specified key or index exists in the array or not. Syntax key_exists ( mixed $key , array $array )  Parameter key(required)- This parameter represents the value to check. array(required)- This parameter signifies an input array with...

1 minute read.

PHP rsort() Function

PHP rsort() Function The rsort () function in PHP is used to sort an array in reverse order i.e., highest to lowest. Syntax rsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) Parameter array(required)- This parameter represents the input array. sort_flags (optional)- This parameter is used to...

1 minute read.

PHP quoted_printable_encode() Function

PHP quoted_printable_encode() Function The quoted_printable_encode() function in PHP converts a 8 bit string to a quoted-printable string. This function is similar to imap_8bit(), except this one does not require the IMAP module to work. Syntax quoted_printable_encode ( string $str ) Parameter str(required)- This parameter represents...

1 minute read.

PHP var_export() Function

PHP var_export() Function The var_export() function in PHP is used to get the structured information for the specified variable. This function outputs or returns a parsable string representation of a variable. This function is similar to...

1 minute read.

PHP String get_html_translation_table() Function

The get_html_translation_table() function in PHP is used to return the translation table used by the htmlentities() and htmlspecialchars() functions. Syntax get_html_translation_table ([ int $table = HTML_SPECIALCHARS [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = "UTF-8" ]]] ) Parameter table (optional)- This parameter specifies which translation table to return. It...

2 minutes read.

PHP vsprintf() Function

PHP vsprintf() Function The vsprintf() function in PHP returns a formatted string. This function accepts an array of arguments. Syntax vsprintf ( string $format , array $args ) Parameter format(required)- This parameter specifies the string and how to format the variables in it. The conversion specification of this parameter...

3 minutes read.

Python Set pop() method

Python Set pop() method The set.pop() method in Python removes an arbitrary element (random item) from the set and returns the element removed. Syntax set.pop()  Parameter NA Return This method returns an arbitrary (random) element from the set. Example 1 #...

1 minute read.