USORT |
Tri utilisateur |
---|---|
PHP 4.0+ |
Syntaxe
function usort ($array, $cmp_function); |
Paramètres
Nom | Description |
---|---|
$array | Ce paramètre permet d'indiquer le tableau à traiter. |
$cmp_function | Ce paramètre permet d'indiquer la fonction utilisateur pour effectuer la comparaison |
Retour
Valeur | Description |
---|---|
tableau | Ces valeurs permettent d'indiquer le tableau traité. |
Description
Cette fonction permet d'effectuer une tri des éléments d'un tableau en utilisant une fonction utilisateur.
Exemple
Prenons pour acquis le répertoire suivant situé à l'emplacement «F:/Inetpub/wwwroot/test/» :
2011-03-24 09:29 52 acos.php 2011-03-29 10:04 102 acosh.php 2011-03-21 16:08 97 alert.htm 2011-03-29 12:58 500 array.php 2011-03-24 09:35 54 asin.php 2011-03-24 09:46 109 atan.php 2011-03-30 14:24 232 atan2.php 2011-03-24 09:54 356 ceil.php 2010-03-23 11:59 3 461 checkcmyk.php 2010-03-23 09:05 611 105 CMYK.jpg 2011-03-24 10:03 111 cos.php 2011-03-29 09:50 100 cosh.php 2011-03-29 11:35 468 count.php 2011-04-01 14:48 5 638 country.htm 2011-03-21 17:25 741 elementarray.htm 2011-03-29 14:43 130 end.php 2011-03-24 10:10 99 exp.php 2011-03-29 11:20 281 explode.php 2011-03-24 10:18 372 floor.php 2010-02-25 12:54 94 header1.php 2011-03-29 14:02 329 isset.php 2011-03-24 10:26 101 log.php 2011-03-21 15:35 254 mathabs.htm 2011-03-21 15:46 101 mathabs1.htm 2011-03-22 14:16 97 mathacos.htm 2011-03-22 14:28 101 mathasin.htm 2011-03-22 15:01 159 mathatan.htm 2011-03-24 09:05 486 mathceil.htm 2011-03-23 14:30 170 mathcos.htm 2011-03-23 14:58 149 mathexp.htm 2011-03-24 10:22 524 mathfloor.htm 2011-03-23 15:29 134 mathlog.htm 2011-03-23 16:03 383 mathmax.htm 2011-03-23 16:12 383 mathmin.htm 2011-03-23 16:21 476 mathpower.htm 2011-03-23 16:33 336 mathrandom.htm 2011-03-24 09:00 502 mathround.htm 2011-03-24 08:49 167 mathsin.htm 2011-03-24 09:11 181 mathsqrt.htm 2011-03-24 09:18 167 mathtan.htm 2011-03-24 10:33 290 max.php 2011-03-24 10:39 290 min.php 2011-03-29 13:48 49 mkdir.php 2009-08-14 08:30 42 164 normal.jpg 2010-03-02 15:32 12 826 normal2.jpg 2009-08-24 10:00 786 531 normal3.jpg 2011-03-24 10:48 340 pow.php 2011-03-24 10:56 372 round.php 2011-03-24 11:02 108 sin.php 2011-03-24 11:08 115 sqrt.php 2011-03-29 10:52 213 strlen.php 2011-03-29 13:38 147 strtolower.php 2011-03-29 13:34 147 strtoupper.php 2010-03-31 13:09 1 334 substr.html 2011-03-29 11:06 134 substr.php 2011-03-24 11:13 108 tan.php 2011-03-29 10:15 100 tanh.php 2011-03-23 10:24 808 upload1.htm 2011-04-05 15:38 597 usort.php 2011-03-21 16:02 129 var.htm |
Voici un exemple permettant d'effectuer le tri de ce répertoire par ordre de date :
- <?php
- function fileCompareInterne($a,$b) {
- $cmpA = filemtime($a);
- $cmpB = filemtime($b);
- if($cmpA == $cmpB) return 0;
- return ($cmpA < $cmpB) ? -1 : 1;
- }
-
- $open_dir = @opendir("F:/Inetpub/wwwroot/test/");
-
- if( (!$open_dir) || (empty($open_dir)) ) {
- echo "Répetoire introuvable";
- exit();
- }
-
- $fileCount = 0;
- while ( $file = readdir($open_dir)) {
- if( (!is_dir($file)) and (!empty($file)) ) {
- $fileList[$fileCount] = $file;
- $fileCount++;
- }
- }
- sort($fileList,SORT_STRING);
- usort($fileList, "fileCompareInterne");
-
-
- closedir($open_dir);
- var_dump($fileList);
- ?>
on obtiendra le résultat suivant :
array(60) {[0]=>
string(10) "normal.jpg"
[1]=>
string(11) "normal3.jpg"
[2]=>
string(11) "header1.php"
[3]=>
string(11) "normal2.jpg"
[4]=>
string(8) "CMYK.jpg"
[5]=>
string(13) "checkcmyk.php"
[6]=>
string(11) "substr.html"
[7]=>
string(11) "mathabs.htm"
[8]=>
string(12) "mathabs1.htm"
[9]=>
string(7) "var.htm"
[10]=>
string(9) "alert.htm"
[11]=>
string(16) "elementarray.htm"
[12]=>
string(12) "mathacos.htm"
[13]=>
string(12) "mathasin.htm"
[14]=>
string(12) "mathatan.htm"
[15]=>
string(11) "upload1.htm"
[16]=>
string(11) "mathcos.htm"
[17]=>
string(11) "mathexp.htm"
[18]=>
string(11) "mathlog.htm"
[19]=>
string(11) "mathmax.htm"
[20]=>
string(11) "mathmin.htm"
[21]=>
string(13) "mathpower.htm"
[22]=>
string(14) "mathrandom.htm"
[23]=>
string(11) "mathsin.htm"
[24]=>
string(13) "mathround.htm"
[25]=>
string(12) "mathceil.htm"
[26]=>
string(12) "mathsqrt.htm"
[27]=>
string(11) "mathtan.htm"
[28]=>
string(8) "acos.php"
[29]=>
string(8) "asin.php"
[30]=>
string(8) "atan.php"
[31]=>
string(8) "ceil.php"
[32]=>
string(7) "cos.php"
[33]=>
string(7) "exp.php"
[34]=>
string(9) "floor.php"
[35]=>
string(13) "mathfloor.htm"
[36]=>
string(7) "log.php"
[37]=>
string(7) "max.php"
[38]=>
string(7) "min.php"
[39]=>
string(7) "pow.php"
[40]=>
string(9) "round.php"
[41]=>
string(7) "sin.php"
[42]=>
string(8) "sqrt.php"
[43]=>
string(7) "tan.php"
[44]=>
string(8) "cosh.php"
[45]=>
string(9) "acosh.php"
[46]=>
string(8) "tanh.php"
[47]=>
string(10) "strlen.php"
[48]=>
string(10) "substr.php"
[49]=>
string(11) "explode.php"
[50]=>
string(9) "count.php"
[51]=>
string(9) "array.php"
[52]=>
string(14) "strtoupper.php"
[53]=>
string(14) "strtolower.php"
[54]=>
string(9) "mkdir.php"
[55]=>
string(9) "isset.php"
[56]=>
string(7) "end.php"
[57]=>
string(9) "atan2.php"
[58]=>
string(11) "country.htm"
[59]=>
string(9) "usort.php"
}
Dernière mise à jour : Dimanche, le 5 avril 2015