这是我的多重排序代码。
但是,我如何为个别颜色做这件事
到目前为止,我所做的是
PHP代码:
<?php
$a1=array("Cat","Alpha","two");
$a1=array("Cat","Beta","two");
$a1=array("Pluto","Beta","two");
$a1=array("Pluto","Alpha","two");
array_multisort($a1[0], SORT_ASC, SORT_STRING, $a1[1], SORT_NUMERIC, SORT_DESC);
$json[] = $a1;
$final=array("response"=>$a1);
echo json_encode($final);
?>
它将显示为
Array ( [0] => Cat [1] => Dog [2] => zb ) Array ( [0] => Missy [1] => Pluto [2] => Fido )
但是,我想显示
作为Asc的$ a1的第一个coloumn,然后作为Desc的$ a1的第二个coloumn,
所以结果应该是
Col A Col B, Col C
Cat, Alpha two
Cat, Beta two
Pluto, Alpha two
Plugo, Beta two
我该怎么做?
答案 0 :(得分:1)
我想您想使用usort来构建自己的自定义排序算法:
<?php
$array = array(
array("Cat","Alpha","two"),
array("Cat","Beta","two"),
array("Pluto","Beta","two"),
array("Pluto","Alpha","two"),
);
function my_custom_sort($a, $b)
{
// first sort ASC by first column
if ($a[0] == $b[0]) {
// then sort ASC by second column
if ($a[1] == $b[1]) {
return 0;
} else {
return $a[1] < $b[1] ? -1 : 1;
}
} else {
return $a[0] < $b[0] ? -1 : 1;
}
}
usort($array, 'my_custom_sort');
var_dump($array);
答案 1 :(得分:0)
<?php
$a1=array("Dog","zb","Cat");
$a2=array("Pluto","Fido","Missy");
array_multisort($a1,SORT_ASC);
array_multisort($a2,SORT_DESC);
print_r($a1);
print_r($a2);
?>