对数组进行数字排序

发布于 2024-08-13 03:20:03 字数 6341 浏览 4 评论 0 原文

我有一个需要排序的 PHP 数组。我已经包含了下面的示例数组。我需要将前 10 个 URL 及其透视计数放入不同的数组中。我知道如果没有 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
        )

I have a PHP array that I need to sort. I have included the example array below. I need to put the top 10 number of URLS plus their perspective counts in a different array. I know I could run into problem if there aren't 10 top matches ... if that happens then a random matching would be fine.

Any suggestions?

I have tried sort(myarray) but that just sorts the first object in the array I need it to sort the second.

Any ideas?


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
        )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼睛会笑 2024-08-20 03:20:03

您需要编写一个自定义排序函数 - 像这样:

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

You need to write a custom sorting function - like this:

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

Then you pass that function into a sort - like this:

usort($myArray, "MyCustomSort");

You could also write a function to help you sort by website - like this:

function MyCustomSort($a, $b)
{
    if ($a->name == $b->name) {
        return 0;
    }
    return ($a->name < $b->name) ? -1 : 1;
}
半枫 2024-08-20 03:20:03

您可以使用装饰-排序-取消装饰模式。

<?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);

它是如何运作的?

不是对项目数组进行排序,而是对数组数组进行排序,其中最后一个元素是项目,第一个元素是要排序的键。排序后,您只保留该项目。

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

按多个字段排序

您可以使用多个排序键,例如按计数排序,如果计数相同,则考虑 url。您可以通过按重要性顺序装饰多个键来实现此目的,如下所示:

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

为什么它这么快

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

You may use decorate-sort-undecorate pattern.

<?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);

How does it work?

Instead of sorting array of your items you sort array of arrays whose last element is item and first is the key by which you want to sort. After sorting you keep only the item.

You can use just sort for sorting array of arrays because PHP compares two same length arrays by comparing its elements one by one.

Sorting by multiple fields

You may use more then one sort key, for example sort by count and if counts are identical take url into account. You can do this by decorating with multiple keys with order of importance like so:

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

Why it's fast

This way might be faster that using usort because it calls your custom code only n-times for sorting array of length n. Comparisons during sort are done using built in comparator so should be fast. In usort method custom comparator is called multiple times (more than n times) during sort and it may slow down things.

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

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

usort($myarray, "sortByCount");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文