Current Location: Home> Latest Articles> How to uniformly format strings in an array in combination with strnatcasecmp and array_map?

How to uniformly format strings in an array in combination with strnatcasecmp and array_map?

gitbox 2025-05-27

In daily PHP programming, we often need to sort string arrays, but the default dictionary sorting ( sort , usort , etc.) does not handle strings with numbers well. For example, if file names such as img1.png , img2.png , img10.png are compared with ordinary strings, the sorting result will be img1.png , img10.png , img2.png , which obviously does not conform to human natural sorting habits.

In order to achieve "natural sorting", PHP provides strnatcmp and strnatcasecmp functions, corresponding to case-sensitive and case-insensitive natural sorting, respectively.
In addition, when we sort the array, we often need to format each element in the array uniformly, such as turning all lowercase or removing unnecessary spaces. At this time, we can use array_map to achieve it.

This article will introduce how to combine these two tools, format the strings in the array uniformly, and then sort them naturally.

Core steps

1?? Prepare data array <br> Let's take a set of file names as an example:

 $files = ['Img10.png', 'img2.png', 'IMG1.png', 'img20.png', 'img11.png'];

2?? Format elements using array_map <br> Suppose we want to convert them all to lowercase:

 $formattedFiles = array_map('strtolower', $files);

If you want to deal with more complex things (such as de-spaces and unified extensions), you can write anonymous functions:

 $formattedFiles = array_map(function($item) {
    return strtolower(trim($item));
}, $files);

3?? Custom sorting function
PHP's usort allows us to sort with custom functions:

 usort($formattedFiles, 'strnatcasecmp');

Note: strnatcasecmp is a comparison function, not a function that is used directly for sorting.

Complete sample code

 <?php
$files = ['Img10.png', 'img2.png', 'IMG1.png', 'img20.png', 'img11.png'];

// first step:Unified format(Turn lowercase + Remove spaces)
$formattedFiles = array_map(function($item) {
    return strtolower(trim($item));
}, $files);

// Step 2:Natural sorting(Case insensitive)
usort($formattedFiles, 'strnatcasecmp');

// Output result
foreach ($formattedFiles as $file) {
    echo $file . "\n";
}
?>

Running results:

 img1.png
img2.png
img10.png
img11.png
img20.png

More practical example: situation with URL

Suppose we are dealing with a set of URLs:

 $urls = [
    'https://gitbox.net/File10.html',
    'https://gitbox.net/file2.html',
    'https://gitbox.net/FILE1.html',
    'https://gitbox.net/file20.html',
    'https://gitbox.net/file11.html',
];

We want to sort by file name part, we can extract the file name first and then sort:

 <?php
$urls = [
    'https://gitbox.net/File10.html',
    'https://gitbox.net/file2.html',
    'https://gitbox.net/FILE1.html',
    'https://gitbox.net/file20.html',
    'https://gitbox.net/file11.html',
];

// Extract the file name part and bind it to URL
$mapped = array_map(function($url) {
    $parts = parse_url($url);
    $file = basename($parts['path']);
    return ['url' => $url, 'file' => strtolower($file)];
}, $urls);

// 按文件名Natural sorting
usort($mapped, function($a, $b) {
    return strnatcasecmp($a['file'], $b['file']);
});

// Output sorted URL
foreach ($mapped as $item) {
    echo $item['url'] . "\n";
}
?>

Output:

 https://gitbox.net/FILE1.html
https://gitbox.net/file2.html
https://gitbox.net/File10.html
https://gitbox.net/file11.html
https://gitbox.net/file20.html