Current Location: Home> Latest Articles> How to Sort an Array by String Length Using rsort() in PHP?

How to Sort an Array by String Length Using rsort() in PHP?

gitbox 2025-09-11

In PHP, the rsort() function is commonly used to sort arrays in descending order, but it sorts based on the values of array elements by default. If we want to sort an array by the length of its strings, rsort() cannot achieve this directly. To sort an array by string length, we can use usort() combined with a custom comparison function, or preprocess the array by calculating string lengths before sorting.

This article will show how to use rsort() to sort an array by string length, along with code examples and explanations.

Using rsort() with strlen() for Descending String Length Sorting

First, it's important to understand that rsort() cannot directly sort by string length, as it sorts based on the values of array elements. Therefore, we need to use usort() to sort arrays by string length.

However, usort() itself sorts arrays in ascending order. We can define a custom comparison function that compares the lengths of strings to sort by length. We can also set it to descending order to achieve an effect similar to rsort().

Example Code:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Example array</span></span><span>
</span><span><span class="hljs-variable">$array</span></span><span> = [</span><span><span class="hljs-string">"apple"</span></span><span>, </span><span><span class="hljs-string">"banana"</span></span><span>, </span><span><span class="hljs-string">"kiwi"</span></span><span>, </span><span><span class="hljs-string">"grapes"</span></span><span>, </span><span><span class="hljs-string">"strawberry"</span></span><span>];
<p></span>// Custom comparison function to sort by string length<br>
function compareByLength($a, $b) {<br>
return strlen($b) - strlen($a);  // Sort in descending order<br>
}</p>
<p>// Sort the array using usort and the custom function<br>
usort($array, 'compareByLength');</p>
<p>// Output the sorted array<br>
print_r($array);<br>
?><br>
</span>

Code Explanation:

  1. Define the array: First, we define an array $array containing names of various fruits, which include strings of different lengths.

  2. Custom comparison function: compareByLength() is a custom function that compares the lengths of two strings. Here, we use strlen($b) - strlen($a) to sort in descending order by length. The strlen() function returns the length of a string, and $b and $a are the strings being compared. A positive return value indicates that $b is longer than $a, and a negative value indicates the opposite.

  3. Sort the array: The usort() function sorts the array according to the rules defined by the custom comparison function compareByLength.

  4. Output the sorted result: Finally, we use print_r() to display the sorted array, which will be ordered from the longest to the shortest string.

Sorting Result:

After running the above code, the output will be:

<span><span><span class="hljs-title function_ invoke__">Array</span></span><span>
(
    [</span><span><span class="hljs-number">0</span></span><span>] => strawberry
    [</span><span><span class="hljs-number">1</span></span><span>] => banana
    [</span><span><span class="hljs-number">2</span></span><span>] => grapes
    [</span><span><span class="hljs-number">3</span></span><span>] => apple
    [</span><span><span class="hljs-number">4</span></span><span>] => kiwi
)
</span></span>

As you can see, the elements with longer string lengths appear first.

Conclusion

Although rsort() cannot directly sort by string length, we can achieve this using a custom comparison function with usort(). By calculating string lengths with strlen() in the comparison function, we can sort arrays by length. This method not only preserves the descending order effect of rsort(), but also allows flexible sorting based on specific requirements.

Hopefully, this method helps you manage arrays more effectively in PHP!