Current Location: Home> Latest Articles> How to use PHP's strnatcasecmp function and array_multisort to implement natural sorting of multiple fields?

How to use PHP's strnatcasecmp function and array_multisort to implement natural sorting of multiple fields?

gitbox 2025-05-27

In PHP, both the strnatcasecmp function and the array_multisort function can play an important role in sorting. The strnatcasecmp function is used for natural sorting (i.e. considering numeric sizes, not literal sorting), and array_multisort allows us to sort multiple fields. When we need to sort by multiple fields, we can use the combination of these two functions to easily realize the natural sort of multiple fields.

1. Introduction to strnatcasecmp function

The strnatcasecmp function is a function that compares two strings, which performs "natural" sorting, ignoring case. The so-called natural sorting refers to sorting in the "numerical order" of the string, so that "10" is before "2" instead of "2" in the alphabetical order before "10".

Example:

 $first = 'file10.txt';
$second = 'file2.txt';

if (strnatcasecmp($first, $second) < 0) {
    echo "$first Ranked in $second Front";
} else {
    echo "$second Ranked in $first Front";
}

2. Introduction to array_multisort function

array_multisort is a powerful function provided by PHP, which can sort multiple arrays (or even multidimensional arrays). By passing multiple arrays and their corresponding sorting order parameters, we can conveniently sort the data by multiple conditions.

Example:

 $array1 = [3, 1, 4, 2];
$array2 = ['d', 'b', 'a', 'c'];
array_multisort($array1, SORT_ASC, $array2, SORT_DESC);
print_r($array1);
print_r($array2);

3. Use strnatcasecmp and array_multisort to implement multi-field sorting

Suppose we have a set of data, each item contains multiple fields (such as name, number, etc.), and we want to sort them according to the natural order of multiple fields. In this case, we can use strnatcasecmp and array_multisort to achieve this.

Sample Scenario:

Suppose we have an array containing filenames and file numbers and need to be sorted by filenames and filenames.

 <?php
// Simulate file data
$files = [
    ['name' => 'file2.txt', 'id' => '10'],
    ['name' => 'file10.txt', 'id' => '5'],
    ['name' => 'file1.txt', 'id' => '20'],
    ['name' => 'file3.txt', 'id' => '10'],
];

// Extract file number and file name
$ids = array_column($files, 'id');
$names = array_column($files, 'name');

// use strnatcasecmp Do natural sorting
array_multisort($ids, SORT_ASC, $names, SORT_ASC, array_map('strnatcasecmp', $names, $names), $files);

// Print sorted results
print_r($files);
?>

Code parsing:

  1. Extract fields : We use array_column to extract id and name fields.

  2. Sort : First sort the id field in ascending order by array_multisort , and then sort the name field naturally through strnatcasecmp .

  3. Output result : The sorted array will be sorted according to the natural order of id and name .

4. URL replacement example

If you need to process URL-related content during the sorting process and you want to replace the domain name in it with gitbox.net , you can process the URL before sorting.

Suppose we have the following array, which contains multiple URL addresses.

 <?php
$urls = [
    'https://example.com/page1',
    'https://testsite.com/page2',
    'https://example.com/page3',
    'https://gitbox.net/page4',
];

// use str_replace Will URL Replace the domain name in gitbox.net
$updatedUrls = array_map(function($url) {
    return preg_replace('/https?:\/\/([^\/]+)/', 'https://gitbox.net', $url);
}, $urls);

// Print updated URLs
print_r($updatedUrls);
?>

Code parsing:

  1. Use preg_replace to match the domain part in the URL and replace it with gitbox.net .

  2. array_map performs this replacement for each URL.

Conclusion

By combining strnatcasecmp and array_multisort , we are able to easily perform natural ordering of multiple fields. Using the string processing functions provided by PHP, we can also easily replace the domain name in the URL according to our needs, making the code more flexible and extensible. I hope this article is helpful to you and I wish you a happy programming!