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 the array elements by default. If we want to sort an array by string length, rsort() cannot directly achieve this. To sort an array by string length, we can combine usort() with a custom comparison function, or calculate the string lengths before sorting.

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

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

First, it’s important to understand that rsort() cannot directly sort by string length—it sorts based on element values. Therefore, we need to use usort() to achieve sorting by string length.

However, usort() sorts arrays in ascending order by default. We can define a custom comparison function that compares strings by length, achieving length-based sorting. The sorting can be set to descending order to mimic the behavior of 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 class="hljs-string">"banana"</span></span>, </span><span><span class="hljs-string">"kiwi"</span></span>, </span><span><span class="hljs-string">"grapes"</span></span>, </span><span><span class="hljs-string">"strawberry"</span></span>];
<p></span>// Custom comparison function to sort by string length<br>
function compareByLength($a, $b) {<br>
return strlen($b) - strlen($a);  // Descending order<br>
}</p>
<p>// Sort 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. Defining the array: We first define an array $array containing different fruit names, with strings of varying 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 by descending length. The strlen() function returns the length of a string, and $b and $a are the two strings being compared. A positive return value means $b is longer than $a, and a negative value indicates the opposite.

  3. Sorting the array: The usort() function sorts the array based on the rules defined in the comparison function compareByLength.

  4. Outputting the result: Finally, print_r() outputs the sorted array, showing the strings arranged from longest to shortest.

Sorting Result:

After executing the code above, 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 strings appear first.

Conclusion

Although rsort() cannot directly sort by string length, we can achieve this using a custom comparison function with usort(). By using strlen() in the comparison function, we can sort an array based on string length. This approach preserves the descending order effect of rsort() while providing flexibility to sort array elements according to custom rules.

This method should help you better manipulate arrays in PHP!