Php 按三个值对多维数组排序
您好,我如何根据3个值对多维数组进行排序,这3个值是ps\u kind、ps\u date和ps\u premium。 对于任何答案或片段,我都会心存感激Php 按三个值对多维数组排序,php,arrays,sorting,Php,Arrays,Sorting,您好,我如何根据3个值对多维数组进行排序,这3个值是ps\u kind、ps\u date和ps\u premium。 对于任何答案或片段,我都会心存感激 Array ( [0] => Array ( [ps_date] => 2013-08-05 20:56:33 [ps_kind] => Gold [ps_premium] = > 1 ) [1] =
Array
(
[0] => Array
(
[ps_date] => 2013-08-05 20:56:33
[ps_kind] => Gold
[ps_premium] = > 1
)
[1] => Array
(
[ps_date] => 2013-08-05 20:46:33
[ps_kind] => Gold
[ps_premium] = > 0
)
[2] => Array
(
[ps_date] => 2013-08-05 20:16:33
[ps_kind] => Silver
[ps_premium] = > 0
)
[3] => Array
(
[ps_date] => 2013-08-05 20:06:33
[ps_kind] => Bronze
[ps_premium] = > 0
)
)
我试过这个代码,但他工作不正常
function cmp($a, $b)
{
$pos = array (
'Gold' => 1,
'Silver' => 2,
'Bronze' => 3,
);
list ($a1, $c1) = explode('', $a['ps_kind']);
list ($a2, $c2) = explode('', $b['ps_kind']);
$catcmp = strcmp(trim($c1), trim($c2));
if ($catcmp==0)
return $pos[trim($a1)] - $pos[trim($a2)];
else return $catcmp;
}
$result = $this -> db ->query($query);
foreach ( $result as $element ) {
$array[] = $element;
}
usort($array,'cmp');
$index = 0;
foreach ($array as $single ) {
$count = $index + 1;
if($array[$index]['ps_date'] < $array[$count]['ps_date'] && $array[$index]['ps_kind'] == $array[$count]['ps_kind'] ) {
$prev = $array[$index];
$next = $array[$count];
$array[$index] = $next;
$array[$count] = $prev;
}
$index++;
}
函数cmp($a,$b)
{
$pos=阵列(
“黄金”=>1,
“银色”=>2,
“青铜色”=>3,
);
列表($a1,$c1)=爆炸(“”,$a['ps_kind');
列表($a2,$c2)=爆炸('''$b['ps_kind');
$catcmp=strcmp(纵倾($c1),纵倾($c2));
如果($catcmp==0)
返回$pos[trim($a1)]-$pos[trim($a2)];
否则返回$catcmp;
}
$result=$this->db->query($query);
foreach($result作为$element){
$array[]=$element;
}
usort($array,'cmp');
$index=0;
foreach($single形式的数组){
$count=$index+1;
如果($array[$index]['ps\u date']<$array[$count]['ps\u date']&&$array[$index]['ps\u kind']==$array[$count]['ps\u kind'])){
$prev=$array[$index];
$next=$array[$count];
$array[$index]=$next;
$array[$count]=$prev;
}
$index++;
}
像这样的东西怎么样:
将$a
替换为$a['ps_-kind']
(与$b相同)和$a['ps_-kind']=$b['ps_-kind']
您可以在ps_-date中再次添加相同的函数,因此如果ps_-kind在ps_-date之前是相同的排序(与ps_-date相同=>ps_-premium)
如果您在尝试=>后对此有疑问,请在此处进行注释:)如果使用,则可以使用用户定义的函数对数组进行排序。此函数必须返回-1、0或1。下面的示例代码将首先测试ps_种类,如果它们相同,则将测试ps_premium。这将首先用“青铜”进行分类
function test( $a, $b ) {
$pos = Array( 'Gold' => 1, 'Silver' => 2, 'Bronze' => 3 );
if( $pos[$a['ps_kind']] < $pos[$b['ps_kind']] ) {
return 1;
} else if( $a['ps_kind'] == $b['ps_kind'] ) {
if( $a['ps_premium'] < $b['ps_premium'] ) {
return 1;
} else if( $a['ps_premium'] == $b['ps_premium'] ) {
return 0;
} else {
return -1;
}
} else {
return -1;
}
}
$x = Array( Array
(
ps_date => "2013-08-05 20:56:33",
ps_kind => "Gold",
ps_premium => 1
), Array
(
ps_date => "2013-08-05 20:06:33",
ps_kind => "Bronze",
ps_premium => 0
), Array
(
ps_date => "2013-08-05 20:46:33",
ps_kind => "Gold",
ps_premium => 0
), Array
(
ps_date => "2013-08-05 20:16:33",
ps_kind => "Silver",
ps_premium => 0
)
);
usort( $x, "test" );
var_dump( $x );
功能测试($a,$b){
$pos=阵列('Gold'=>1,'Silver'=>2,'brown'=>3);
如果($pos[$a['ps\u kind']]<$pos[$b['ps\u kind']])){
返回1;
}否则如果($a['ps\u kind']=$b['ps\u kind'])){
如果($a['ps_溢价]<$b['ps_溢价]){
返回1;
}否则如果($a['ps_premium']=$b['ps_premium'])){
返回0;
}否则{
返回-1;
}
}否则{
返回-1;
}
}
$x=数组(数组)
(
ps_date=>“2013-08-05 20:56:33”,
ps_kind=>“黄金”,
ps_premium=>1
),数组
(
ps_日期=>“2013-08-05 20:06:33”,
ps_kind=>“青铜”,
ps_premium=>0
),数组
(
ps_date=>“2013-08-05 20:46:33”,
ps_kind=>“黄金”,
ps_premium=>0
),数组
(
ps_date=>“2013-08-05 20:16:33”,
ps_kind=>“银色”,
ps_premium=>0
)
);
usort($x,“测试”);
var_dump(x美元);
这不是一个可以免费申请代码的网站!向我们展示您的尝试,我们将帮助您。我无意要求提供代码bcz,因为我是“或代码片段”编写的,所以我认为有人可以编写线索或其他东西,我想您可以与自定义函数一起使用。