Current Location: Home> Latest Articles> How to Prevent the natsort Function from Changing the Original Array Keys During Sorting

How to Prevent the natsort Function from Changing the Original Array Keys During Sorting

gitbox 2025-08-30

In PHP, the natsort function is a commonly used method for sorting arrays according to "natural order." This sorting style aligns better with human reading habits—for instance, the number "10" will come after "2" rather than following the traditional ASCII order. However, when using natsort for sorting, you may encounter a problem—it resets the array keys, causing the original keys to be lost. This can make it challenging to access data according to the original keys in subsequent operations.

This article will discuss how to prevent natsort from changing the original array keys and introduce some related tips and alternative methods.

natsort and Its Effects

The natsort function sorts an array based on "natural order." Natural order sorting means numbers are ordered by their actual value rather than by string dictionary order. For example:

<span><span><span class="hljs-variable">$array</span></span><span> = [</span><span><span class="hljs-string">'a10'</span></span><span>, </span><span><span class="hljs-string">'a2'</span></span><span>, </span><span><span class="hljs-string">'a1'</span></span><span>, </span><span><span class="hljs-string">'a20'</span></span><span>];
</span><span><span class="hljs-title function_ invoke__">natsort</span></span><span>(</span><span><span class="hljs-variable">$array</span></span><span>);
</span><span><span class="hljs-title function_ invoke__">print_r</span></span><span>(</span><span><span class="hljs-variable">$array</span></span><span>);
</span></span>

After executing the above code, the output is as follows:

<span><span><span class="hljs-title function_ invoke__">Array</span></span><span>
(
    [</span><span><span class="hljs-number">2</span></span><span>] => a1
    [</span><span><span class="hljs-number">1</span></span><span>] => a2
    [</span><span><span class="hljs-number">0</span></span><span>] => a10
    [</span><span><span class="hljs-number">3</span></span><span>] => a20
)
</span></span>

You can see that natsort indeed sorts the array according to numerical value, but it resets the array keys, so the keys originally associated with the elements are no longer preserved.

How to Prevent Key Loss

If you want to preserve the original keys when using natsort, you can save the array keys before sorting and then rebuild the array after sorting. This ensures that the sorting process does not affect the original keys.

Method 1: Using array_values and natsort

You can extract the array values for sorting while keeping the keys intact. This is done using the array_values function. The approach is to save the original keys first and then sort:

<span><span><span class="hljs-variable">$array</span></span><span> = [</span><span><span class="hljs-string">'a'</span></span><span> => </span><span><span class="hljs-string">'a10'</span></span><span>, </span><span><span class="hljs-string">'b'</span></span><span> => </span><span><span class="hljs-string">'a2'</span></span><span>, </span><span><span class="hljs-string">'c'</span></span><span> => </span><span><span class="hljs-string">'a1'</span></span><span>, </span><span><span class="hljs-string">'d'</span></span><span> => </span><span><span class="hljs-string">'a20'</span></span><span>];
<p></span>// Save original keys<br>
$original_keys = array_keys($array);</p>
<p>// Sort values naturally<br>
natsort($array);</p>
<p>// Restore original keys<br>
$array = array_combine($original_keys, $array);</p>
<p>print_r($array);<br>
</span>

Output:

<span><span><span class="hljs-title function_ invoke__">Array</span></span><span>
(
    [a] => a1
    [b] => a2
    [c] => a10
    [d] => a20
)
</span></span>

In this method, the original keys are first obtained using array_keys, and the array is sorted using natsort. After sorting, array_combine restores the association between the keys and the sorted values.

Method 2: Using uasort for Custom Sorting

Another approach is to use the uasort function for custom sorting. Similar to natsort, uasort can sort arrays, but it does not change the keys. By implementing a custom sorting function, you can control the sorting method while preserving the original keys:

<span><span><span class="hljs-variable">$array</span></span><span> = [</span><span><span class="hljs-string">'a'</span></span><span> => </span><span><span class="hljs-string">'a10'</span></span><span>, </span><span><span class="hljs-string">'b'</span></span><span> => </span><span><span class="hljs-string">'a2'</span></span><span>, </span><span><span class="hljs-string">'c'</span></span><span> => </span><span><span class="hljs-string">'a1'</span></span><span>, </span><span><span class="hljs-string">'d'</span></span><span> => </span><span><span class="hljs-string">'a20'</span></span><span>];
<p></span>uasort($array, function($a, $b) {<br>
return strnatcmp($a, $b);<br>
});</p>
<p>print_r($array);<br>
</span>

uasort sorts the array via a callback function. Inside the callback, strnatcmp is used to simulate natural order sorting without changing the array keys.

Output:

<span><span><span class="hljs-title function_ invoke__">Array</span></span><span>
(
    [c] => a1
    [b] => a2
    [a] => a10
    [d] => a20
)
</span></span>

Conclusion

Although natsort is a powerful sorting function, it resets the array keys. If you need to preserve the keys during sorting, you can use one of the following two methods:

  1. Save the original keys first, and then restore them with array_combine after sorting.

  2. Use the uasort function for custom sorting to avoid losing keys.

Using these two methods ensures that the array retains its original keys after natural sorting, preventing data loss during the sorting process.