You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP实现英文数字单词字符串排序程序的技术需求

PHP Solution for Sorting English Number Words

Got it, let's build a PHP program that takes a string of English number words, sorts them (ascending or descending), and outputs the sorted words back. Here's how to do it properly:

Step 1: Create a Number-Word Mapping

First, we need a way to convert between English words and their numeric values. We'll use an associative array for this—easy to look up and reverse later.

// Mapping of English number words to their numeric values
$numberWords = [
    'one' => 1,
    'two' => 2,
    'three' => 3,
    'four' => 4,
    'five' => 5,
    'six' => 6,
    'seven' => 7,
    'eight' => 8,
    'nine' => 9,
    'ten' => 10,
    // Add more numbers here if you need to handle larger values
];

// Reverse the mapping to convert numbers back to words
$numericToWords = array_flip($numberWords);

Step 2: Handle Input and Clean Up

Next, we'll take the input string, split it into individual words, and clean up any extra whitespace (in case the user adds spaces after commas like five, two, seven).

// Example input (you can replace this with user input via $_POST/$_GET or CLI)
$input = "five,two,seven,ten,four";

// Split input into array and trim whitespace from each element
$inputWords = array_map('trim', explode(',', $input));

Step 3: Convert Words to Numbers

Now we'll convert each word in our input array to its corresponding numeric value. We'll also add a check to skip any invalid words (in case the input has typos or unrecognized terms).

$numericValues = [];
foreach ($inputWords as $word) {
    // Only process words that exist in our mapping
    if (isset($numberWords[strtolower($word)])) {
        $numericValues[] = $numberWords[strtolower($word)];
    }
}

Step 4: Sort the Values

Choose either ascending or descending sort. We'll show both options here:

// Ascending sort (smallest to largest)
sort($numericValues);

// OR Descending sort (largest to smallest)
// rsort($numericValues);

Step 5: Convert Back to Words and Output

Finally, we'll convert the sorted numbers back to English words and join them into a comma-separated string.

$sortedWords = [];
foreach ($numericValues as $number) {
    $sortedWords[] = $numericToWords[$number];
}

// Join into a string and output
$output = implode(',', $sortedWords);
echo $output;

Full Working Example

Putting it all together, here's the complete code that works with your sample input:

<?php
$numberWords = [
    'one' => 1,
    'two' => 2,
    'three' => 3,
    'four' => 4,
    'five' => 5,
    'six' => 6,
    'seven' => 7,
    'eight' => 8,
    'nine' => 9,
    'ten' => 10,
];
$numericToWords = array_flip($numberWords);

$input = "five,two,seven,ten,four";
$inputWords = array_map('trim', explode(',', $input));

$numericValues = [];
foreach ($inputWords as $word) {
    if (isset($numberWords[strtolower($word)])) {
        $numericValues[] = $numberWords[strtolower($word)];
    }
}

// Use sort() for ascending, rsort() for descending
sort($numericValues);

$sortedWords = [];
foreach ($numericValues as $number) {
    $sortedWords[] = $numericToWords[$number];
}

echo implode(',', $sortedWords); // Output: two,four,five,seven,ten
?>

Notes

  • Case Insensitivity: The code uses strtolower() so it handles inputs like Five, TWO correctly.
  • Invalid Input Handling: Any unrecognized words are skipped—you can modify this to throw an error or notify the user if needed.
  • Scalability: Just add more entries to the $numberWords array if you need to handle numbers beyond ten (like eleven, twelve, etc.).

内容的提问来源于stack exchange,提问作者Arbab Khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:53:29