0

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

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

        }
4

2 回答 2

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 );
于 2013-10-18T19:48:15.680 回答
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 => ps_premium 相同)

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

于 2013-10-18T19:37:11.073 回答