1

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

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

我正在考虑将每一行拆分为多维数组并对其进行排序,但我被卡住了,也许有一种完全不同的方式。

谢谢!

4

1 回答 1

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);

或者,更简洁地说:

$sorted_string = implode(' ', sort(explode(' ', $input)));

如果默认值sort()不能给你你想要的,你应该检查一下 usort() 函数

于 2009-10-31T12:38:26.660 回答