English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Dans ce tutoriel, vous apprendrez à trier les éléments ou les clés des tableaux en ordre croissant ou décroissant en PHP.
Dans le chapitre précédent, vous avez appris les bases des tableaux PHP, à savoir ce qu'est un tableau, comment les créer, comment visualiser leur structure, comment accéder à leurs éléments, etc. Vous pouvez faire plus avec les tableaux, par exemple trier les éléments selon l'ordre que vous spécifiez.
PHP est livré avec de nombreuses fonctions intégrées, spécialement conçues pour trier les éléments des tableaux de différentes manières, par exemple en ordre alphabétique ou numérique, en ordre croissant ou décroissant. Ici, nous allons explorer quelques fonctions les plus couramment utilisées pour le tri des tableaux.
sort() et rsort() - Trier les tableaux d'indices
asort() et arsort() - Utilisés pour trier les tableaux associatifs par valeur
ksort() et krsort() - Utilisés pour trier les tableaux associatifs par clé
La fonction sort() est utilisée pour trier les éléments d'un tableau d'indices en ordre croissant (les lettres sont triées alphabétiquement, et les nombres sont triés numériquement).
<?php //Define an array $colors = array("Red", "Green", "Blue", "Yellow"); //Sort and print the array sort($colors); print_r($colors); ?>Test and see‹/›
The print_r() statement provides the following output:
Array ( [0] => Blue [1] => Green [2] => Red [3] => Yellow )
De même, vous pouvez trier les éléments numériques du tableau en ordre croissant.
<?php //Define an array $numbers = array(1, 2, 2.5, 4, 7, 10); //Sort and print the array sort($numbers); print_r($numbers); ?>Test and see‹/›
The print_r() statement provides the following output:
Array ( [0] => 1 [1] => 2 [2] => 2.5 [3] => 4 [4] => 7 [5] => 10 )
La fonction rsort() est utilisée pour trier les éléments d'un tableau d'indices en ordre décroissant (les lettres sont triées alphabétiquement, et les nombres sont triés numériquement).
<?php //Define an array $colors = array("Red", "Green", "Blue", "Yellow"); // Sort and print the array rsort($colors); print_r($colors); ?>Test and see‹/›
The print_r() statement provides the following output:
Array ( [0] => Yellow [1] => Red [2] => Green [3] => Blue )
Similarly, you can sort the numeric elements of an array in descending order.
<?php //Define an array $numbers = array(1, 2, 2.5, 4, 7, 10); //Sort and print the array rsort($numbers); print_r($numbers); ?>Test and see‹/›
The print_r() statement provides the following output:
Array ( [0] => 10 [1] => 7 [2] => 4 [3] => 2.5 [4] => 2 [5] => 1 )
The asort() function sorts the elements of an associative array in ascending order by value. It works similarly to sort(), but it preserves the association between keys and their values during sorting.
<?php //Define an array $age = array("Peter"=>20, "Harry"=>14, "John"=>45, "Clark"=>35); //Sort and print the array by value asort($age); print_r($age); ?>Test and see‹/›
The print_r() statement provides the following output:
Array ( [Harry] => 14 [Peter] => 20 [Clark] => 35 [John] => 45 )
The arsort() function sorts the elements of an associative array in descending order by value. It works similarly to rsort(), but it preserves the association between keys and their values during sorting.
<?php //Define an array $age = array("Peter"=>20, "Harry"=>14, "John"=>45, "Clark"=>35); //Sort and print the array by value arsort($age); print_r($age); ?>Test and see‹/›
The print_r() statement provides the following output:
Array ( [John] => 45 [Clark] => 35 [Peter] => 20 [Harry] => 14 )
The ksort() function sorts the elements of an associative array in ascending order by key. Like the asort() function, it preserves the association between keys and their values during sorting.
<?php //Define an array $age = array("Peter"=>20, "Harry"=>14, "John"=>45, "Clark"=>35); //Sort the array by key and print ksort($age); print_r($age); ?>Test and see‹/›
The print_r() statement provides the following output:
Array ( [Clark] => 35 [Harry] => 14 [John] => 45 [Peter] => 20 )
The ksort() function sorts the elements of an associative array in ascending order by key. Like the asort() function, it preserves the association between keys and their values during sorting.
<?php //Define an array $age = array("Peter"=>20, "Harry"=>14, "John"=>45, "Clark"=>35); //Sort the array by key and print krsort($age); print_r($age); ?>Test and see‹/›
print_r() statement provides the following output:
Array ( [Peter] => 20 [John] => 45 [Harry] => 14 [Clark] => 35 )