PHP的自定义排序

对服装产品尺码进行排序:

如:XS,X,M,L,XL,XXL,XXL

尺码数据从数据库取出来是无序的,

思路是自定0,1,2这样加上去,以int转化为数值用PHP自带的函数进行排序即可。

switch ($cm->sizes) {
    case 'XS':
        $cmarr[$cm->sizes]='0'.$cm->sizes;
        break;
    case 'S':
        $cmarr[$cm->sizes]='1'.$cm->sizes;
        break;
    case 'M':
        $cmarr[$cm->sizes]='2'.$cm->sizes;
        break;
	case 'L':
        $cmarr[$cm->sizes]='3'.$cm->sizes;
        break;
    case 'XL':
        $cmarr[$cm->sizes]='4'.$cm->sizes;
        break;
    case 'XXL':
        $cmarr[$cm->sizes]='5'.$cm->sizes;
        break;
	case 'XXXL':
        $cmarr[$cm->sizes]='6'.$cm->sizes;
        break;
	default:
		$cmarr[$cm->sizes]=$cm->orders.$cm->sizes;
        break;
}
					
	foreach($cmarr as $str){										
		$str = (int)$str; 										
	}
	sort($cmarr);


你可能感兴趣的:(PHP)