PHP sort() Function – Sorting Arrays

This article explains the PHP sort() function, its signature, optional sorting flags, parameters, return values, and provides a complete example demonstrating how to sort an array of strings in ascending order and display the sorted results.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP sort() Function – Sorting Arrays

The PHP sort() function sorts an array in ascending order, rearranging its elements from lowest to highest.

Signature: bool sort(array &$array, int $sort_flags = SORT_REGULAR). The optional $sort_flags can be one of several constants such as SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_LOCALE_STRING, SORT_NATURAL, or SORT_FLAG_CASE, which modify the comparison behavior.

Parameters: $array – the array to be sorted; $sort_flags – optional flag to change sorting type.

Return value: TRUE on success, FALSE on failure.

Example:

<?php
$fruits = array("lemon","orange","banana","apple");
sort($fruits);
foreach ($fruits as $key => $val) {
    echo "fruits[$key] = $val
";
}
?>

Output:

fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendprogrammingPHPArraySorting
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.