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 に答える