如何对以下php数组进行排序?
问题:如何对以下php数组进行排序?,php,arrays,Php,Arrays,问题: Array ( [0] => 12w12 [1] => 13w12 [2] => 14w12 [3] => 15w12 [4] => 2w13 [5] => 3w13 [6] => 4w13 [7] => 3w12 [8] => 7w12 [9] => 9w12 ) 答案应该是 Array ( [0] => 3w12 [1] => 7w12 [2] => 9w12 [3] => 12w12 [4] =
Array ( [0] => 12w12 [1] => 13w12 [2] => 14w12 [3] => 15w12 [4] => 2w13 [5] => 3w13 [6] => 4w13 [7] => 3w12 [8] => 7w12 [9] => 9w12 )
答案应该是
Array ( [0] => 3w12 [1] => 7w12 [2] => 9w12 [3] => 12w12 [4] => 13w12 [5] => 14w12 [6] => 15w12 [7] => 2w13 [8] => 3w13 [9] =>4w13 )
在php.net上搜索一下usort,然后按照你想要达到的目的制定你自己的排序方法,看起来根本不是标准排序在php.net上搜索一下usort,然后按照你想要达到的目的制定你自己的排序方法,看起来根本不是标准排序你可以使用并编写自己的比较函数。您可以使用和编写自己的比较函数。
函数cmp($a,$b){
function cmp($a, $b){
if ($a == $b) { return 0; }
list($first1, $last1) = explode("w", $a);
list($first2, $last2) = explode("w", $b);
return (($last1.$first1) < ($last2.$first2)) ? -1 : 1;
}
usort($array, "cmp");
如果($a==$b){返回0;}
列表($first1,$last1)=分解(“w”,$a);
列表($first2,$last2)=爆炸($w,$b);
返回(($last1.$first1)<($last2.$first2))?-1:1;
}
usort($数组,“cmp”);
函数cmp($a,$b){
如果($a==$b){返回0;}
列表($first1,$last1)=分解(“w”,$a);
列表($first2,$last2)=爆炸($w,$b);
返回(($last1.$first1)<($last2.$first2))?-1:1;
}
usort($数组,“cmp”);
什么是2w13,为什么在15w12之后?首先按第二个“字段”排序?提示:您只需编写一个比较函数2w13是2013年第2周,15w12是2012年第15周,这就是为什么在15w12之后出现我的答案行吗?什么是2w13,为什么在15w12之后出现?按第二个“字段”排序首先?提示:你只需要写一个比较函数,2W13是2013年的第2周,15w12是2012年的第15周,这就是为什么15w12之后会出现我的答案行吗?