Collator::asort

collator_asort

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL intl >= 1.0.0)

Collator::asort -- collator_asortインデックスの情報を保持しつつ配列を並べ替える

説明

オブジェクト指向型

public Collator::asort(array &$array, int $flags = Collator::SORT_REGULAR): bool

手続き型

collator_asort(Collator $object, array &$array, int $flags = Collator::SORT_REGULAR): bool

この関数は、インデックスと値の関係を保持したうえで配列を並べ替えます。 これを主に使用するのは、実際の並び順も重要となる連想配列です。 配列の要素には、現在のロケールの規則にしたがった並び順が適用されます。

PHP の標準関数 asort() と同等です。

パラメータ

object

Collator オブジェクト。

array

並べ替えたい文字列の配列。

flags

オプションの並べ替え方式。以下のいずれか。

デフォルトの flags の値は Collator::SORT_REGULAR です。 flags に無効な値が指定された場合も、このデフォルトを使用します。

戻り値

成功した場合に true を、失敗した場合に false を返します。

例1 collator_asort() の例

<?php
$coll
= collator_create( 'en_US' );
$arr = array(
'a' => '100',
'b' => '50',
'c' => '7'
);
collator_asort( $coll, $arr, Collator::SORT_NUMERIC );
var_export( $arr );

collator_asort( $coll, $arr, Collator::SORT_STRING );
var_export( $arr );
?>

上の例の出力は以下となります。

array (
  'c' => '7',
  'b' => '50',
  'a' => '100',
)array (
  'a' => '100',
  'b' => '50',
  'c' => '7',
)

参考

add a note

User Contributed Notes 1 note

up
-1
alix dot axel at NOSPAM dot gmail dot com
14 years ago
For those of you who are looking for a way to integrate natural sorting with the UCA rules this hack seems to work:<?php$array = array(    '1', '100',    'al', 'be',    'Alpha', 'Beta',    'Álpha', 'Àlpha', 'Älpha',    'かたかな',    'img1.png', 'img2.png',    'img10.png', 'img20.png');echo '<pre>';print_r(sortIntl($array, true));echo '</pre>';function sortIntl($array, $natural = true){    $data = $array;    if ($natural === true)    {        $data = preg_replace_callback('~([0-9]+)~', 'natsortIntl', $data);    }    collator_asort(collator_create('root'), $data);    return array_intersect_key($array, $data);}function natsortIntl($number){    return sprintf('%032d', $number);}?>Output:Array(    [0] => 1    [1] => 100    [2] => al    [3] => be    [4] => Alpha    [5] => Beta    [6] => Álpha    [7] => Àlpha    [8] => Älpha    [9] => かたかな    [10] => img1.png    [11] => img2.png    [12] => img10.png    [13] => img20.png)
To Top