按三个值对多维数组进行排序

时间:2013-10-18 17:21:22

标签: php arrays sorting

您好我如何根据ps_kind,ps_date,ps_premium这三个值对多维数组进行排序。 我会感谢任何答案或片段。

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++;

        }

2 个答案:

答案 0 :(得分:0)

怎么样......像这样:http://php.net/manual/en/function.usort.php

并将$a替换为$a['ps_kind'](与$ b相同)和$a['ps_kind'] == $b['ps_kind']再次使用ps_date添加相同的函数,这样如果ps_kind与ps_date的排序相同(与ps_date相同) =&gt; ps_premium)

如果您在尝试后对此有疑问=>在此评论:)

答案 1 :(得分:0)

如果使用usort,则可以使用用户定义的函数对数组进行排序。此函数必须返回-1,0或1.以下示例代码将首先测试ps_kind,如果它们相同,则将测试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 );