How to Sort Strings of an Array in Ascending Order in PHP

PHP


In this article, we show how to sort strings of an array in ascending order in PHP.

To sort an array of strings in ascending order in PHP, we use the sort() function.

Let's say we have an array of names below:

$class= array('Mike', 'Bob', 'Steve', 'Craig', 'Richard', 'Adam');

And now we want this array to be shown and ordered in ascending order by name.

We use the sort() function to put it in ascending order.

The general form to put an array of strings in ascending order:

sort($array_name);

For the above array we created, the code to put it in ascending order is:

sort($class);

PHP Output

Adam Bob Craig Mike Richard Steve


HTML Comment Box is loading comments...