In PHP, how do I sort arrays with built-in functions?

In PHP, you can sort arrays using several built-in functions. Here are a few commonly used functions for sorting arrays:

  • sort() - Sorts an array in ascending order.
  • rsort() - Sorts an array in descending order.
  • asort() - Sorts an associative array in ascending order, according to the value.
  • arsort() - Sorts an associative array in descending order, according to the value.
  • ksort() - Sorts an associative array in ascending order, according to the key.
  • krsort() - Sorts an associative array in descending order, according to the key.

Here is an example of sorting a simple array in PHP:

<?php $numbers = array(4, 2, 8, 6, 1); sort($numbers); echo "Sorted numbers: "; print_r($numbers); ?>

In this example, the sort() function is used to sort the array of numbers in ascending order. After sorting, the array is printed to show the sorted order.


PHP array sorting PHP array functions sort arrays in PHP