我有一个像这样的数组:[ ['number' => '419-1'], ['number' => '302-1'], ['number' => '102-1'], ['number' => '102-11'], ['number' => '203-1'], ['number' => '508-1'], ['number' => '105-1'], ['number' => '102-2'], ['number' => '302-2'],]我想要这样的输出:[ ['number' => '102-1'], ['number' => '102-2'], ['number' => '102-11'], ['number' => '105-1'], ['number' => '203-1'], ['number' => '302-1'], ['number' => '302-2'], ['number' => '419-1'], ['number' => '508-1']]问题是数字存储为字符串。我想将数字视为整数。排序需要按数字进行。没有字符串比较。我尝试过usort,但这没有帮助。有人可以帮帮我吗?
2 回答
芜湖不芜
TA贡献1796条经验 获得超7个赞
$arr = [
['number' => '419-1'],
['number' => '419-11'],
['number' => '102-1'],
['number' => '203-1'],
['number' => '508-1'],
['number' => '105-1'],
['number' => '102-2'],
['number' => '302-2'],
];
usort($arr, function($a,$b){
$an = explode('-',$a['number']);
$bn = explode('-',$b['number']);
if($an[0] == $bn[0]){
return $an[1]*1 > $bn[1]*1;
}
return $an[0]*1 > $bn[0]*1;
});
print_r($arr);
- 2 回答
- 0 关注
- 110 浏览
添加回答
举报
0/150
提交
取消