my array is setup as follow:
我的数组设置如下:
array
'testuri/abc' =>
array
'label' => string 'abc' (length=3)
'weight' => float 5
'testuri/abd' =>
array
'label' => string 'abd' (length=3)
'weight' => float 2
'testuri/dess' =>
array
'label' => string 'dess' (length=4)
'weight' => float 2
'testuri/gdm' =>
array
'label' => string 'gdm' (length=3)
'weight' => float 2
'testuri/abe' =>
array
'label' => string 'abe' (length=3)
'weight' => float 2
'testuri/esy' =>
array
'label' => string 'esy' (length=3)
'weight' => float 2
'testuri/rdx' =>
array
'label' => string 'rdx' (length=3)
'weight' => float 3
'testuri/tfc' =>
array
'label' => string 'tfc' (length=3)
'weight' => float 3
I want to get/filter the 5 elements with bigges 'weight'. Is there a php function to make this?
我想得到/过滤5个元素与bigges'权重'。有这个功能吗?
PS. My idea was to use foreach
PS。我的想法是使用foreach
5 个解决方案
#1
As far as I know there isn't.
据我所知,没有。
You could use a heap for this, but for only 5 elements I'm not sure it's faster than just storing the top 5.
你可以使用一个堆,但是对于只有5个元素,我不确定它比仅存储前5个更快。
#2
Sort the array by the weight value in descending order and then get the first five values:
按权重值按降序对数组进行排序,然后获取前五个值:
function cmpByWeight($a, $b) {
return $b['weight'] - $a['weight'];
}
uasort($array, 'cmpByWeight');
$firstFive = array_slice($array, 0, 5);
#3
You'd be better using uasort with a callback that compares the 'weight' index of the passed values, and then array_slice to grab the first 5 elements (or last 5 depending on which way you sort...)
你最好使用带有回调的uasort来比较传递值的'weight'索引,然后使用array_slice来获取前5个元素(或者最后5个元素取决于你排序的方式......)
#4
I would use array_multisort() and then grab the first 5 values.
我会使用array_multisort()然后获取前5个值。
array_multisort($weight, SORT_DESC, $label, SORT_ASC, $YOUR_ARRAY)
array_multisort($ weight,SORT_DESC,$ label,SORT_ASC,$ YOUR_ARRAY)
Then just grab $YOUR_ARRAY[0] - $YOUR_ARRAY[4] or iterate over the array to grab the first 5
然后只需抓住$ YOUR_ARRAY [0] - $ YOUR_ARRAY [4]或迭代数组以获取前5个
[EDIT] here's a link to the function --> http://us3.php.net/manual/en/function.array-multisort.php
[编辑]这里是函数的链接 - > http://us3.php.net/manual/en/function.array-multisort.php
#5
My English is not the best, i will try to explain, what i need. i can sort the array....but in the example above i have following:
我的英语不是最好的,我会尽力解释,我需要什么。我可以对数组进行排序....但在上面的示例中,我有以下内容:
1x weight 5
2x weight 3
5x weight 2
So...if I grab the first 5 elements, the another 3 with weight 2 will be ignored. So i need all 5 elements with weight 2....and so i have an array with 7 items.
所以...如果我抓住前5个元素,另外3个重量为2的元素将被忽略。所以我需要所有5个重量为2的元素....所以我有一个包含7个项目的数组。
Another example:
2x weight 5
4x weight 2
7x weight 1
All elements with weight1 must be ignored, So i get 6 elements in a new array..
必须忽略所有带weight1的元素,所以我在一个新数组中得到6个元素。
#1
As far as I know there isn't.
据我所知,没有。
You could use a heap for this, but for only 5 elements I'm not sure it's faster than just storing the top 5.
你可以使用一个堆,但是对于只有5个元素,我不确定它比仅存储前5个更快。
#2
Sort the array by the weight value in descending order and then get the first five values:
按权重值按降序对数组进行排序,然后获取前五个值:
function cmpByWeight($a, $b) {
return $b['weight'] - $a['weight'];
}
uasort($array, 'cmpByWeight');
$firstFive = array_slice($array, 0, 5);
#3
You'd be better using uasort with a callback that compares the 'weight' index of the passed values, and then array_slice to grab the first 5 elements (or last 5 depending on which way you sort...)
你最好使用带有回调的uasort来比较传递值的'weight'索引,然后使用array_slice来获取前5个元素(或者最后5个元素取决于你排序的方式......)
#4
I would use array_multisort() and then grab the first 5 values.
我会使用array_multisort()然后获取前5个值。
array_multisort($weight, SORT_DESC, $label, SORT_ASC, $YOUR_ARRAY)
array_multisort($ weight,SORT_DESC,$ label,SORT_ASC,$ YOUR_ARRAY)
Then just grab $YOUR_ARRAY[0] - $YOUR_ARRAY[4] or iterate over the array to grab the first 5
然后只需抓住$ YOUR_ARRAY [0] - $ YOUR_ARRAY [4]或迭代数组以获取前5个
[EDIT] here's a link to the function --> http://us3.php.net/manual/en/function.array-multisort.php
[编辑]这里是函数的链接 - > http://us3.php.net/manual/en/function.array-multisort.php
#5
My English is not the best, i will try to explain, what i need. i can sort the array....but in the example above i have following:
我的英语不是最好的,我会尽力解释,我需要什么。我可以对数组进行排序....但在上面的示例中,我有以下内容:
1x weight 5
2x weight 3
5x weight 2
So...if I grab the first 5 elements, the another 3 with weight 2 will be ignored. So i need all 5 elements with weight 2....and so i have an array with 7 items.
所以...如果我抓住前5个元素,另外3个重量为2的元素将被忽略。所以我需要所有5个重量为2的元素....所以我有一个包含7个项目的数组。
Another example:
2x weight 5
4x weight 2
7x weight 1
All elements with weight1 must be ignored, So i get 6 elements in a new array..
必须忽略所有带weight1的元素,所以我在一个新数组中得到6个元素。