To sort an array in ascending order in PHP, you can use the `sort()` function.
In this example,
$numbers
.sort()
function to sort the array in-place.<?php
$numbers = [5, 2, 8, 1, 9];
sort($numbers);
echo 'Sorted Array of Integers: ' . implode(', ', $numbers);
?>
Sorted Array of Integers: 1, 2, 5, 8, 9
In this example,
$words
.sort()
function to sort the array in-place based on string comparison.<?php
$words = ['apple', 'banana', 'cherry', 'date'];
sort($words);
echo 'Sorted Array of Strings: ' . implode(', ', $words);
?>
Sorted Array of Strings: apple, banana, cherry, date
In this tutorial, we learned How to Sort an Array in Ascending Order in PHP language with well detailed examples.