(PHP 4, PHP 5)
Trie un tableau suivant les clés avec l'algorithme à "ordre naturel".
knatsort() implémente un algorithme de tri qui traite les clés du tableau array comme un être humain tout en conservant la relation clé/valeur. C'est ce qui est appelé l'"ordre naturel". Un exemple de la différence de traitement entre un tel algorithme et un algorithme de tri de chaînes (comme lorsqu'on utilise ksort()) est illustré ci-dessous.
<?php function knatsort( &$array ) { return uksort($array, "strnatcmp"); } ?>
bool knatsort( array &$array )
Cette fonction retourne TRUE en cas de succès ou FALSE si une erreur survient.
Exemple #1 Exemple avec knatsort()
<?php $array1 = $array2 = array("img12.png"=>"12", "img10.png"=>"10", "img2.png"=>"2", "img1.png"=>"1"); ksort($array1); echo "Standard sorting\n"; print_r($array1); knatsort($array2); echo "\nNatural order sorting\n"; print_r($array2); ?>
L'exemple ci-dessus va afficher :
Standard sorting Array ( [img1.png] => 1 [img10.png] => 10 [img12.png] => 12 [img2.png] => 2 ) Natural order sorting Array ( [img1.png] => 1 [img2.png] => 2 [img10.png] => 10 [img12.png] => 12 )
Commentaire(s)
Poster un commentaire