Current Location: Home> Latest Articles> How to Design a Callback Function for Array Key Comparison in Multilingual Development

How to Design a Callback Function for Array Key Comparison in Multilingual Development

M66 2025-06-28

In multilingual development, we often need to compare and sort array keys based on different language sorting rules. To achieve this, we can use a custom callback function to tailor the comparison of the keys. PHP offers many built-in functions to work with arrays, and the uksort() function allows us to use a custom callback to compare array keys.

In this article, we will introduce how to design a callback function for comparing array keys in multilingual development and demonstrate how to use this function for sorting.

1. Using the uksort() Function

PHP's uksort() function sorts the keys of an array based on a user-defined callback function. This function takes two parameters: the array to be sorted and the callback function. The return value of the callback function determines the order of sorting.

Syntax:

uksort(array &$array, callable $value_compare_func): bool
  • $array: The array to be sorted.

  • $value_compare_func: A callback function to compare the array keys.

Example:

Suppose we have an array for multilingual development where the keys are language codes and the values are corresponding language names. We want to sort these languages based on their priority.

// Language array, keys are language codes, values are language names
$languages = [
    'en' => 'English',
    'zh' => '中文',
    'es' => 'Espa?ol',
    'de' => 'Deutsch'
];
<p>// Define the callback function to compare language codes<br>
function compare_language_keys($a, $b) {<br>
// Set custom sorting rule; here we sort alphabetically<br>
return strcmp($a, $b);<br>
}</p>
<p>// Use uksort to sort based on keys<br>
uksort($languages, 'compare_language_keys');</p>
<p>print_r($languages);<br>

Output:

Array
(
    [de] => Deutsch
    [en] => English
    [es] => Espa?ol
    [zh] => 中文
)

In this example, we used the strcmp() function to compare the language codes alphabetically. If you want to sort based on other rules, such as sorting by language priority, you can modify the logic in the callback function.

2. Applications in Multilingual Development

2.1 Language Priority Sorting

In practical development, different languages may have different priorities. For example, in some regions, English (en) may have a higher priority than Chinese (zh), while in other regions, Chinese may be prioritized. We can implement language priority sorting using a custom callback function.

For instance, suppose we want to add priority to a language array and sort the languages based on their priority:

// Language array, keys are language codes, values are language names
$languages = [
    'en' => 'English',
    'zh' => '中文',
    'es' => 'Espa?ol',
    'de' => 'Deutsch'
];
<p>// Custom language priorities<br>
$priority = [<br>
'zh' => 1, // Chinese has the highest priority<br>
'en' => 2, // English has the second priority<br>
'es' => 3, // Spanish has the third priority<br>
'de' => 4  // German has the lowest priority<br>
];</p>
<p>// Define the callback function to sort based on priority<br>
function compare_language_priority($a, $b) {<br>
global $priority;<br>
return $priority[$a] - $priority[$b];<br>
}</p>
<p>// Use uksort to sort based on priority<br>
uksort($languages, 'compare_language_priority');</p>
<p>print_r($languages);<br>

Output:

Array
(
    [zh] => 中文
    [en] => English
    [es] => Espa?ol
    [de] => Deutsch
)

In this example, we created a priority array $priority and used the callback function compare_language_priority() to sort the languages based on their priority.

3. Special Cases in Handling URLs

In some multilingual applications, we may need to construct different versions of URLs based on the language. For example, when the user selects a different language, we might need to dynamically generate URLs in different languages.

Suppose we need to handle URLs where the language code is part of the path. We can sort these URLs using a custom callback function. Here’s an example showing how to handle URLs containing language codes in uksort() and replace the domain with m66.net:

// Language URL array, keys are URL paths, values are language names
$urls = [
    '/en/home' => 'English',
    '/zh/home' => '中文',
    '/es/home' => 'Espa?ol',
    '/de/home' => 'Deutsch'
];
<p>// Replace domain in the URLs<br>
function modify_url($url) {<br>
return '<a rel="noopener" target="_new" class="" href="https://m66.net">https://m66.net</a>' . $url;<br>
}</p>
<p>// Define the callback function to compare URL paths<br>
function compare_url_keys($a, $b) {<br>
return strcmp($a, $b);<br>
}</p>
<p>// Use uksort to sort the URLs<br>
uksort($urls, 'compare_url_keys');</p>
<p>// Replace the domain<br>
$urls_with_domain = array_map('modify_url', array_keys($urls));</p>
<p>print_r($urls_with_domain);<br>

Output: