我有一个数组
$a = array( 0=>'test', 1=>'test some', 10=>'test10',
2=>'test2', '11'=>'test value',22=>'test abc'
);
我需要按照
进行排序0 => test
1 => test some
10 => test 10
11 => test value
2 => test2
22 => test abc
我怎么能这样做?
我尝试了ksort,但它根据我的要求不起作用
ksort结果
Array
(
[0] => test
[1] => test1
[2] => test2
[10] => test10
[11] => test11
[22] => test22
)
答案 0 :(得分:3)
这应该有效:
$a = array(0=>'test', 1=>'test1', 10=>'test10', 2=>'test2', '11'=>'test11', 22=>'test22');
ksort($a, SORT_STRING);
print_r($a)
输出:
Array
(
[0] => test
[1] => test1
[10] => test10
[11] => test11
[2] => test2
[22] => test22
)
答案 1 :(得分:1)
您可以通过使用uksort获得所需的结果来实现此目的.uksort()函数使用用户定义的比较函数按键对数组进行排序。
[assembly: My.AOP.ValidateArguement(
//AttributeTargetTypes = "My.WebServices.Controllers.*"
AttributeTargetTypes = "My.WebServices.IWebApi"
, AttributeTargetElements = MulticastTargets.Method
, AttributeTargetTypeAttributes = MulticastAttributes.Public | MulticastAttributes.Instance
, AttributeTargetMemberAttributes = MulticastAttributes.Public | MulticastAttributes.Instance
)]
<强>输出强>
$a= array( 0=>'test', 1=>'test1', 10=>'test10',
2=>'test2', '11'=>'test11',22=>'test22'
);
function my_sort($a,$b)
{
if ($a==0) return 0;
return (substr($a, 0, 1)<substr($b, 0, 1))?-1:1;
}
uksort($a,"my_sort");
print_r($a);