How to sort an array in PHP?

Introduction:
PHP offers several sorting functions. sort() sorts an array in ascending order and reindexes its keys.

Example:

<?php
$arr = [3, 1, 4, 1, 5];
sort($arr);
print_r($arr);
?>

Explanation:

  • sort($arr) sorts the array values in ascending order.
  • The keys are reset to numeric indices, starting from 0.
  • print_r($arr) prints the sorted array for debugging.

Leave a Reply

Your email address will not be published. Required fields are marked *