在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!
答
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