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.
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] = orangeSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
