在PHP中对包含字母和数字的字符串进行排序

在PHP中对包含字母和数字的字符串进行排序

问题描述:

I am pulling through variations(options) of a product. Each variation/option has a different name/code and sometimes it contains a mixture of numbers and letters.

I would like to know if there is a way I can sort this in a neat way.

Please see picture attach of how the variations are currently listed.

Thanks in advance!

enter image description here

我正在浏览产品的变体(选项)。 每个变体/选项都有不同的名称/代码,有时它包含数字和字母的混合。 p>

我想知道是否有一种方法可以整齐地对其进行排序。 p>

请参见附图如何变化 目前已列出。 p>

提前致谢! p>

p> div >

A sort() can do that for you. Here's an example from the PHP page doing pretty much the same thing you are trying to do:

$fruits = array(
    "Orange1", "orange2", "Orange3", "orange20"
);

sort($fruits, SORT_NATURAL | SORT_FLAG_CASE);

foreach ($fruits as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "
";
}

This example will output:

fruits[0] = Orange1
fruits[1] = orange2
fruits[2] = Orange3
fruits[3] = orange20