HomeLanguagesPHP | collator_asort() Function

PHP | collator_asort() Function

The collator_asort() function is an inbuilt function in PHP which is used to sort array maintaining the index association. This function sorts an array such that array indices maintain their correlation with the array elements they are associated with. The array elements are sorted according to current locale rules. Syntax:

  • Procedural style:
bool collator_asort( $coll, &$arr, $sort_flag )
  • Object oriented style:
public bool Collator::asort( &$arr, $sort_flag )

Parameters: This function accepts three parameters as mentioned above and described below:

  • $coll: This parameter is used as collator object.
  • $arr: This parameter contains the array of strings which need to sort.
  • $sort_flag: It is optional parameter which is used to define the sorting method, one of the following:
    • Collator::SORT_REGULAR: It compare items normally. It is the default sorting.
    • Collator::SORT_NUMERIC: It compare items numerically.
    • Collator::SORT_STRING: It compare items as strings.

Return Value: This function returns True on success or False on failure. Below programs illustrate the collator_asort() function in PHP: Program 1: 

php




<?php
$coll = collator_create( 'en_US' );
$arr = array(
     'A' => '30',
     'B' => '48',
     'C' => '9',
     'D' => '60'
);
 
// Sort array according to its numeral value
collator_asort( $coll, $arr, Collator::SORT_NUMERIC );
var_export( $arr );
?>


Output:

array (
  'C' => '9',
  'A' => '30',
  'B' => '48',
  'D' => '60',
)

Program 2: 

php




<?php
$coll = collator_create( 'en_US' );
$arr = array(
     'A' => '30',
     'B' => '48',
     'C' => '9',
     'D' => '60'
);
 
// Sort array according to its string value
collator_asort( $coll, $arr, Collator::SORT_STRING );
var_export( $arr );
?>


Output:

array (
  'A' => '30',
  'B' => '48',
  'D' => '60',
  'C' => '9',
)

Related Articles:

Reference: http://php.net/manual/en/collator.asort.php

RELATED ARTICLES

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS