基于2个值对数组进行排序

时间:2014-08-16 00:01:21

标签: php arrays

我创建了一个镜像系统,人们可以为链接投票(工作/破碎) 该数组看起来像这样

Array
(
    [0] => Array
        (
            [id] => 1
            [link] => link.com/file.zip
            [filename] => file.zip
            [good] => 10
            [broken] => 5
        )

    [1] => Array
        (
            [id] => 2
            [link] => link.com/file.zip
            [filename] => file.zip
            [good] => 10
            [broken] => 9
        )

        [2] => Array
        (
            [id] => 3
            [link] => link.com/file.zip
            [filename] => file.zip
            [good] => 6
            [broken] => 0
        )

        [3] => Array
        (
            [id] => 4
            [link] => link.com/file.zip
            [filename] => file.zip
            [good] => 0
            [broken] => 0
        )
        [4] => Array
            (
                [id] => 5
                [link] => link.com/file.zip
                [filename] => file.zip
                [good] => 2
                [broken] => 5
            )

我尝试做的是基于此排序链接: 得分最高的链接显示在最顶层 我想像这样计算得分: 得分 =好 - 不好

如果分数为负,则会显示没有投票的链接,因此该系统的结果将为:

数组2,0,1,3,4

1 个答案:

答案 0 :(得分:0)

使用自定义排序:function.usort。 'compare'函数计算'good'和'broken'计数的差异作为'score'。然后比较'得分'并返回+1,0或-1的适当“指标”。

经测试:PHP 5.3.18 Demonstration at viper-7

代码:

// sort the array based on the only the difference between the 'good' and 'broken' scores.
// to force descending sort then reverse the indicator returned by the comparison function.
usort($source, function ($e1, $e2) {
                   $e1Score = $e1['good'] - $e1['broken'];
                   $e2Score = $e2['good'] - $e2['broken'];

                   if ($e1Score < $e2Score) { return +1; }
                   if ($e1Score > $e2Score) { return -1; }
                   return 0;
                }
               );

测试数据:

$source = Array(
              Array('id' => 1, 'link' => 'link.com/file.zip', 'filename' => 'file.zip', 'good' => 10, 'broken' => 5),
              Array('id' => 2, 'link' => 'link.com/file.zip', 'filename' => 'file.zip', 'good' => 10, 'broken' => 9),
              Array('id' => 3, 'link' => 'link.com/file.zip', 'filename' => 'file.zip', 'good' =>  6, 'broken' => 0),
              Array('id' => 4, 'link' => 'link.com/file.zip', 'filename' => 'file.zip', 'good' =>  0, 'broken' => 0),
              Array('id' => 5, 'link' => 'link.com/file.zip', 'filename' => 'file.zip', 'good' =>  2, 'broken' => 5),
);

分类输出:

Array
(
    [0] => Array
        (
            [id] => 3
            [link] => link.com/file.zip
            [filename] => file.zip
            [good] => 6
            [broken] => 0
        )

    [1] => Array
        (
            [id] => 1
            [link] => link.com/file.zip
            [filename] => file.zip
            [good] => 10
            [broken] => 5
        )

    [2] => Array
        (
            [id] => 2
            [link] => link.com/file.zip
            [filename] => file.zip
            [good] => 10
            [broken] => 9
        )

    [3] => Array
        (
            [id] => 4
            [link] => link.com/file.zip
            [filename] => file.zip
            [good] => 0
            [broken] => 0
        )

    [4] => Array
        (
            [id] => 5
            [link] => link.com/file.zip
            [filename] => file.zip
            [good] => 2
            [broken] => 5
        )

)