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 is the role of interfaces in PHP and how can they be utilized to create more flexible and reusable code structures when working with multiple classes?
- What are the potential pitfalls of using PHP to include SVG code inline in a document?
- What potential issue is indicated by the error message "Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource" in the PHP code?