[英]PHP - Split multidimensional array unique values
Think if I start with an explanation first it will help... I have a competition in which there are 3 winners in each County (The ones with the highest votes). 想一想,如果我先从一个解释开始会有所帮助。。。我有一个竞赛,每个县有3个优胜者(票数最高的人)。
My current array looks like this: 我当前的数组如下所示:
Array
(
[0] => Array
(
[entryID] => 1
[votes] => 3
[countyID] => 46
)
[1] => Array
(
[entryID] => 4
[votes] => 1
[countyID] => 2
)
[2] => Array
(
[entryID] => 2
[votes] => 0
[countyID] => 46
)
[3] => Array
(
[entryID] => 5
[votes] => 0
[countyID] => 46
)
)
What I need to do here is figure out a way of finding the top 3 highest votes within each of the CountyID's. 我在这里需要做的是找出在每个CountyID中获得前3名最高投票的方法。
Any ideas how I can achieve this? 有什么想法可以实现这一目标吗? Thanks, Scott.
谢谢,斯科特。
The simplest way to do it is to re-organize your array so the country id is the top level index, and then just write a simple custom function to sort the vote count in descending order... that way the top voted entries are on top. 最简单的方法是重新组织数组,使国家/地区ID为顶级索引,然后编写一个简单的自定义函数以按降序对投票计数进行排序...最佳。
$entries = array(
array('entryId' => 1, 'votes' => 3, 'countryId' => 46),
array('entryId' => 4, 'votes' => 1, 'countryId' => 2),
array('entryId' => 2, 'votes' => 0, 'countryId' => 46),
array('entryId' => 5, 'votes' => 0, 'countryId' => 46),
);
// Sort votes in descending order (most on top)
function voteSort($a, $b) {
if ($a['votes'] == $b['votes']) {
return 0;
}
return ($a['votes'] < $b['votes']) ? 1 : -1;
}
// Re-organize the array with country as top level
$byCountry = array();
foreach ($entries as $entry) {
$byCountry[$entry['countryId']][] = array(
'entryId' => $entry['entryId'],
'votes' => $entry['votes']
);
}
// For each country, sort by votes
foreach ($byCountry as $index => $country) {
usort($byCountry[$index], 'voteSort');
}
That should work. 那应该工作。
The function ksort()
will be helpful. 函数
ksort()
将很有帮助。
You can sort your array firstly with the rank and second the country. 您可以先按排名对国家/地区排序,然后再对国家/地区排序。 So you will have all of the firsts in every country on the begining of your array, then the seconds, etc.
因此,在阵列开始时,您将拥有每个国家/地区的所有首创记录,然后是秒数,依此类推。
The function you will pass to ksort will be kind of 您将传递给ksort的功能将是
function compareVote($a,$b){
if($a['votes'] > $b['votes'])
return 1;
if($a['votes'] == $b['votes']){
if($a['countyID'] > $b['countyID'])
return 1;
if($a['countyID'] == $b['countyID'])
return 0;
return -1;
}
return -1;
}
View the official doc page . 查看官方文档页面 。
You can split the array by unique country IDs in this way: 您可以通过以下方式按唯一的国家/地区ID拆分阵列:
$entries = array(
array('entryID' => 1, 'votes' => 3, 'countryID' => 46),
array('entryID' => 4, 'votes' => 1, 'countryID' => 2),
array('entryID' => 2, 'votes' => 0, 'countryID' => 46),
array('entryID' => 5, 'votes' => 0, 'countryID' => 46),
);
$entriesByCountry = array();
foreach($entries as $entry) {
if(!array_key_exists($entry->countyID, $entriesByCountry)
$entriesByCountry[$entry->countyID] = array();
$entriesByCountry[$entry->countyID][] = $entry;
}
Than sort each country-array by votes and take the first 3. 比对每个国家/地区阵列进行投票表决,然后进行前3个投票。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.