我有一个像这样的片段容量
$array1 = array("1 Go", "1 To", "2 To", "500 Go", "320 Go")
当我对它进行排序时,它会给出:
1 Go
1 To
2 To
320 Go
500 Go
我想要的是:
1 Go
320 Go
500 Go
1 To
2 To
我试过使用natsort()函数,它给出与asort()相同的结果:
1 Go
1 To
2 To
320 Go
500 Go
如何在PHP中执行此操作?
答案 0 :(得分:0)
$array1 = array("1 Go", "1 To", "2 To", "500 Go", "320 Go");
$go = $to = array();
foreach($array1 as $v){
if(strpos($v, 'Go') !== false){
$go[] = $v;
}elseif(strpos($v, 'To') !== false){
$to[] = $v;
}
}
sort($to);
sort($go);
$array1 = array_merge($go,$to);
print_r($array1);
结果:
Array ( [0] => 1 Go [1] => 320 Go [2] => 500 Go [3] => 1 To [4] => 2 To )
答案 1 :(得分:0)
试试他的:
$array1 = array("1 Go", "1 To", "2 To", "500 Go", "320 Go");
$toArray = array();
$goArray = array();
foreach ($array1 as $item)
{
if (strpos($item, "Go")) array_push($goArray, $item);
else array_push($toArray, $item);
}
asort($toArray);
asort($goArray);
array_merge($goArray, $toArray);