Which PHP functions can be used to sort arrays in ascending or descending order?
To sort arrays in PHP in ascending or descending order, you can use the `sort()` function to sort in ascending order and `rsort()` function to sort in descending order. These functions modify the original array in place. Example:
// Sorting array in ascending order
$numbers = array(4, 2, 8, 6);
sort($numbers);
print_r($numbers); // Output: Array ( [0] => 2 [1] => 4 [2] => 6 [3] => 8 )
// Sorting array in descending order
rsort($numbers);
print_r($numbers); // Output: Array ( [0] => 8 [1] => 6 [2] => 4 [3] => 2 )
Related Questions
- What are some common pitfalls to avoid when trying to save form data from an HTML table using PHP?
- Welche Überlegungen sollte man bei der Gestaltung der Datenbankstruktur anstellen, um Redundanzen zu vermeiden und die Effizienz der Datenbankabfragen zu verbessern?
- How can PHP routing be implemented to improve the organization of PHP applications and avoid unnecessary file redirections?