数字排序数组

编程入门 行业动态 更新时间:2024-10-09 04:23:50
本文介绍了数字排序数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个需要排序的PHP数组。我在下面包括了示例数组。我需要将排名前10位的URLS及其透视数放入另一个数组中。我知道如果没有10个顶级匹配项,我可能会遇到问题...如果发生这种情况,则可以进行随机匹配。

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.

有什么建议吗?

我尝试过sort(myarray),但这只是对数组中的第一个对象进行排序,而我需要对第二个对象进行排序。

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

有什么想法吗?

Array ( [0] => Array ( [name] => bit.ly/2oUTzf [count] => 1 ) [1] => Array ( [name] => tiny.cc/wyNbi [count] => 1 ) [2] => Array ( [name] => ow.ly/Almo [count] => 1 ) [3] => Array ( [name] => bit.ly/7bQ8sY [count] => 1 ) [4] => Array ( [name] => kissa.be/w4V- [count] => 5 ) [5] => Array ( [name] => ow.ly/xzwI [count] => 1 ) [6] => Array ( [name] => twa.lk/L6FZX [count] => 1 ) [7] => Array ( [name] => tinyurl/Alyssa10 [count] => 1 ) [8] => Array ( [name] => www.hiderefer/0cz7kNgA.htm [count] => 1 ) [9] => Array ( [name] => tinyurl/Joanie515 [count] => 1 ) [10] => Array ( [name] => ow.ly/uJvB [count] => 1 ) [11] => Array ( [name] => tinyurl/ [count] => 1 ) [12] => Array ( [name] => www.hiderefer/wJBUhh3G.htm [count] => 1 ) [13] => Array ( [name] => short.to/xcxc [count] => 1 ) [14] => Array ( [name] => bit.ly/t79FA [count] => 2 ) [15] => Array ( [name] => tinyurl/yzy33yl [count] => 1 ) [16] => Array ( [name] => p.gs/zksz6 [count] => 1 ) [17] => Array ( [name] => bit.ly/7E1cc8 [count] => 1 ) [18] => Array ( [name] => bit.ly/6hbugu [count] => 1 ) [19] => Array ( [name] => tii.libsyn/index.php [count] => 6 ) [20] => Array ( [name] => tinyurl/nlzzwq [count] => 1 ) [21] => Array ( [name] => bit.ly/7gAdXi [count] => 1 ) [22] => Array ( [name] => localtweeps [count] => 1 ) [23] => Array ( [name] => localtweeps. [count] => 3 ) [24] => Array ( [name] => scribd/doc/22365778 [count] => 1 ) [25] => Array ( [name] => quick-weight-loss-secrets/ [count] => 1 ) [26] => Array ( [name] => tinyurl/ykd5qm5 [count] => 1 ) [27] => Array ( [name] => bit.ly/5DQ6SO [count] => 1 ) [28] => Array ( [name] => bit.ly/4z6Kww [count] => 1 ) [29] => Array ( [name] => bit.ly/40sm9N [count] => 1 ) [30] => Array ( [name] => bit.ly/8mh7DO [count] => 5 ) [31] => Array ( [name] => tinyurl/krt5yf [count] => 1 ) [32] => Array ( [name] => bit.ly/7GsthV [count] => 1 ) [33] => Array ( [name] => bit.ly/1QJzvM [count] => 1 ) [34] => Array ( [name] => yfrog/1durkj [count] => 1 ) [35] => Array ( [name] => budurl/dxwc [count] => 9 ) [36] => Array ( [name] => digg/d1qiCr [count] => 1 ) [37] => Array ( [name] => bit.ly/eVSIo [count] => 1 ) [38] => Array ( [name] => yfrog/37badgj [count] => 2 ) [39] => Array ( [name] => tinyurl/qh8sos [count] => 1 ) [40] => Array ( [name] => tinyurl/mz7l8d [count] => 3 ) [41] => Array ( [name] => tinyurl/nratac [count] => 1 ) [42] => Array ( [name] => tinyurl/yk587jx [count] => 1 ) [43] => Array ( [name] => www.bethel.edu/alumni/homecoming/09/events/ [count] => 1 ) [44] => Array ( [name] => www.waytofit [count] => 1 ) [45] => Array ( [name] => twitpic/rdcy8 [count] => 1 ) [46] => Array ( [name] => retwt.me/1C1Vd [count] => 14 ) [47] => Array ( [name] => www.starbucks/card [count] => 1 ) [48] => Array ( [name] => tinyurl/yhkbfqe [count] => 13 ) [49] => Array ( [name] => bit.ly/playspy [count] => 1 ) [50] => Array ( [name] => bit.ly/57rHLO [count] => 12 )

推荐答案

您需要编写自定义排序功能-像这样:

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

更多推荐

数字排序数组

本文发布于:2023-11-29 06:14:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1645575.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!