在PHP中订购字母数字列表

1 php alphanumeric

嗨,我需要在PHP中订购一个如下所示的列表:B1200 120A81 00A12 00A22 C100B C100C

有序列表将是:00A12 00A22 120A81 B1200 C100B C100C

我正在考虑拆分多维数组中的每一行并对它进行排序但是我被卡住了,也许这是完全不同的方式.

谢谢!

Jer*_*gan 6

如果正常的排序功能可以执行您想要的操作,那么拆分/排序很容易:

// break up the string into an array on spaces
$new_array = explode(' ', $input);
// sort the array
sort($new_array);
// put the string back together
$sorted_string = implode(' ', $new_array);
Run Code Online (Sandbox Code Playgroud)

或者,更简洁:

$sorted_string = implode(' ', sort(explode(' ', $input)));
Run Code Online (Sandbox Code Playgroud)

如果默认设置sort()不能满足您的要求,请查看usort()函数.

  • 我想补充一点,你甚至可以使用"natsort"功能. (2认同)