Collator::asort

collator_asort

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

Collator::asort -- collator_asort — Trie un tableau en conservant les clés, avec une collation

Description

Style orienté objet

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

Style procédural

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

Cette fonction trie un tableau en maintenant l'association entre les clés et les valeurs. Elle sert surtout à trier des tableaux associatifs, où l'ordre des éléments est important. Les éléments seront triés en fonction des conventions locales.

Équivalent de la fonction PHP standard asort() .

Liste de paramètres

object

Objet Collator .

array

tableau de chaînes à trier.

flags

Type de tri, optionnel, choisi parmi les constantes suivantes :

La valeur par défaut de flags est Collator::SORT_REGULAR . Cette valeur est aussi utilisée si une valeur invalide de flags a été fournie.

Valeurs de retour

Cette fonction retourne true en cas de succès ou false si une erreur survient.

Exemples

Exemple #1 Exemple avec 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 );
?>

L'exemple ci-dessus va afficher :

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

Voir aussi

Found A Problem?

Learn How To Improve This Page • Submit a Pull Request • Report a Bug
+add a note

User Contributed Notes 1 note

up
-1
alix dot axel at NOSPAM dot gmail dot com
15 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
)
+add a note

AltStyle によって変換されたページ (->オリジナル) /