PHP - Function asort()



Syntax

asort( $array [, $sort_flags] );

Definition and Usage

This function sorts an array such that array indices maintain their correlation with the array elements they are associated with. This is used mainly when sorting associative arrays where the actual element order is significant.

Parameters

Sr.No Parameter & Description
1

array

Required. Specifies an array.

2

sort_flags

Optional. 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

Returns TRUE on success or FALSE on failure.

Example

Try out following example βˆ’

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

This will produce the following result βˆ’

Array ( [b] => banana [d] => lemon [a] => orange )
php_function_reference.htm
Advertisements