以数字方式对数组进行排序

时间:2009-11-30 08:26:28

标签: php arrays bit.ly sorting

我有一个需要排序的PHP数组。我在下面列出了示例数组。我需要将前10个URLS加上他们的透视计数放在不同的数组中。我知道如果没有10个顶级匹配,我可能遇到问题......如果发生这种情况,那么随机匹配就没问题了。

有什么建议吗?

我已经尝试过sort(myarray),但只是排序数组中的第一个对象,我需要它来排序第二个。

有什么想法吗?


Array
(
    [0] => Array
        (
            [name] => http://bit.ly/2oUTzf
            [count] => 1
        )

    [1] => Array
        (
            [name] => http://tiny.cc/wyNbi
            [count] => 1
        )

    [2] => Array
        (
            [name] => http://ow.ly/Almo
            [count] => 1
        )

    [3] => Array
        (
            [name] => http://bit.ly/7bQ8sY
            [count] => 1
        )

    [4] => Array
        (
            [name] => http://kissa.be/w4V-
            [count] => 5
        )

    [5] => Array
        (
            [name] => http://ow.ly/xzwI
            [count] => 1
        )

    [6] => Array
        (
            [name] => http://twa.lk/L6FZX
            [count] => 1
        )

    [7] => Array
        (
            [name] => http://tinyurl.com/Alyssa10
            [count] => 1
        )

    [8] => Array
        (
            [name] => http://www.hiderefer.com/0cz7kNgA.htm
            [count] => 1
        )

    [9] => Array
        (
            [name] => http://tinyurl.com/Joanie515
            [count] => 1
        )

    [10] => Array
        (
            [name] => http://ow.ly/uJvB
            [count] => 1
        )

    [11] => Array
        (
            [name] => http://tinyurl.com/
            [count] => 1
        )

    [12] => Array
        (
            [name] => http://www.hiderefer.com/wJBUhh3G.htm
            [count] => 1
        )

    [13] => Array
        (
            [name] => http://short.to/xcxc
            [count] => 1
        )

    [14] => Array
        (
            [name] => http://bit.ly/t79FA
            [count] => 2
        )

    [15] => Array
        (
            [name] => http://tinyurl.com/yzy33yl
            [count] => 1
        )

    [16] => Array
        (
            [name] => http://p.gs/zksz6
            [count] => 1
        )

    [17] => Array
        (
            [name] => http://bit.ly/7E1cc8
            [count] => 1
        )

    [18] => Array
        (
            [name] => http://bit.ly/6hbugu
            [count] => 1
        )

    [19] => Array
        (
            [name] => http://tii.libsyn.com/index.php
            [count] => 6
        )

    [20] => Array
        (
            [name] => http://tinyurl.com/nlzzwq
            [count] => 1
        )

    [21] => Array
        (
            [name] => http://bit.ly/7gAdXi
            [count] => 1
        )

    [22] => Array
        (
            [name] => http://localtweeps.com
            [count] => 1
        )

    [23] => Array
        (
            [name] => http://localtweeps.com.
            [count] => 3
        )

    [24] => Array
        (
            [name] => http://scribd.com/doc/22365778
            [count] => 1
        )

    [25] => Array
        (
            [name] => http://quick-weight-loss-secrets.com/
            [count] => 1
        )

    [26] => Array
        (
            [name] => http://tinyurl.com/ykd5qm5
            [count] => 1
        )

    [27] => Array
        (
            [name] => http://bit.ly/5DQ6SO
            [count] => 1
        )

    [28] => Array
        (
            [name] => http://bit.ly/4z6Kww
            [count] => 1
        )

    [29] => Array
        (
            [name] => http://bit.ly/40sm9N
            [count] => 1
        )

    [30] => Array
        (
            [name] => http://bit.ly/8mh7DO
            [count] => 5
        )

    [31] => Array
        (
            [name] => http://tinyurl.com/krt5yf
            [count] => 1
        )

    [32] => Array
        (
            [name] => http://bit.ly/7GsthV
            [count] => 1
        )

    [33] => Array
        (
            [name] => http://bit.ly/1QJzvM
            [count] => 1
        )

    [34] => Array
        (
            [name] => http://yfrog.com/1durkj
            [count] => 1
        )

    [35] => Array
        (
            [name] => http://budurl.com/dxwc
            [count] => 9
        )

    [36] => Array
        (
            [name] => http://digg.com/d1qiCr
            [count] => 1
        )

    [37] => Array
        (
            [name] => http://bit.ly/eVSIo
            [count] => 1
        )

    [38] => Array
        (
            [name] => http://yfrog.com/37badgj
            [count] => 2
        )

    [39] => Array
        (
            [name] => http://tinyurl.com/qh8sos
            [count] => 1
        )

    [40] => Array
        (
            [name] => http://tinyurl.com/mz7l8d
            [count] => 3
        )

    [41] => Array
        (
            [name] => http://tinyurl.com/nratac
            [count] => 1
        )

    [42] => Array
        (
            [name] => http://tinyurl.com/yk587jx
            [count] => 1
        )

    [43] => Array
        (
            [name] => http://www.bethel.edu/alumni/homecoming/09/events/
            [count] => 1
        )

    [44] => Array
        (
            [name] => http://www.waytofit.net
            [count] => 1
        )

    [45] => Array
        (
            [name] => http://twitpic.com/rdcy8
            [count] => 1
        )

    [46] => Array
        (
            [name] => http://retwt.me/1C1Vd
            [count] => 14
        )

    [47] => Array
        (
            [name] => http://www.starbucks.com/card
            [count] => 1
        )

    [48] => Array
        (
            [name] => http://tinyurl.com/yhkbfqe
            [count] => 13
        )

    [49] => Array
        (
            [name] => http://bit.ly/playspy
            [count] => 1
        )

    [50] => Array
        (
            [name] => http://bit.ly/57rHLO
            [count] => 12
        )

3 个答案:

答案 0 :(得分:2)

您需要编写自定义排序功能 - 如下所示:

function MyCustomSort($a, $b)
{
    if ($a->count == $b->count) {
        return 0;
    }
    return ($a->count < $b->count) ? -1 : 1;
}

然后你将该函数传递给一个排序 - 就像这样:

usort($myArray, "MyCustomSort");

您还可以编写一个函数来帮助您按网站排序 - 如下所示:

function MyCustomSort($a, $b)
{
    if ($a->name == $b->name) {
        return 0;
    }
    return ($a->name < $b->name) ? -1 : 1;
}

答案 1 :(得分:0)

function sortByCount($a, $b)
{
    if ($a['count'] == $b['count']) {
        return 0;
    }
    return ($a['count'] < $b['count']) ? -1 : 1;
}

usort($myarray, "sortByCount");

答案 2 :(得分:0)

您可以使用 decorate-sort-undecorate 模式。

<?php

$arr = Array
(
    [0] => Array
        (
            [name] => http://bit.ly/2oUTzf
            [count] => 1
        )

    [1] => Array
        (
            [name] => http://tiny.cc/wyNbi
            [count] => 1
        )

    [2] => Array
        (
            [name] => http://ow.ly/Almo
            [count] => 1
        )

    [3] => Array
        (
            [name] => http://bit.ly/7bQ8sY
            [count] => 1
        )
...
);

// actual sorting below
$arr= array_map(create_function('$a', 'return array($a["count"], $a);'), $arr); // transform into array of arrays consisted of sort key and item
sort($arr); // sort array of arrays
$arr = array_map('end', $arr); // take only last element from each array

print_r($arr);

它是如何工作的?

不是对项目的数组进行排序,而是对最后一个元素为item的数组进行排序,并且首先是要对其进行排序的键。排序后,您只保留该项目。

您可以只使用sort对数组数组进行排序,因为PHP通过逐个比较其元素来比较两个相同长度的数组。

按多个字段排序

您可以使用多个排序键,例如按计数排序,如果计数相同,则将url考虑在内。您可以通过使用具有重要性顺序的多个键进行装饰来完成此操作:

$arr = array_map(create_function('$a', 'return array($a["count"], $a["name"], $a);'), $arr); 

为什么它很快

这种方式可能比使用usort更快,因为它只调用自定义代码n次以排序长度为n的数组。排序期间的比较是使用内置比较器完成的,因此应该很快。在usort方法中,自定义比较器在排序期间被多次调用(超过n次),这可能会减慢速度。