您好,我目前正在查询从数据库基础用户ID比赛,但是我想避免在我的results_array中选择重复,这个函数getrandomspecies接收array_result,这个数组结果迭代7次。如何测试我不把重复项放在我的results_array中?我得到了几个重复。Php如何检查重复结果?
function getrandomspecies($array_result){
//database connection
$dbn = adodbConnect();
foreach($array_result as $possible){
//query the results
$querys= "select * from taxonomic_units where tsn = $possible";
$resultss = $dbn -> Execute($querys);
while($rowss=$resultss->FetchRow()){
$id = $rowss['tsn']; //there ID
$ranksss = $rowss['rank_id']; //ranking id, I choose 220 and 230
if($ranksss == 220 || $ranksss == 230){
$prelimary_array[] = $id;
}
}
//grab random index
$index = array_rand($prelimary_array,1);
//put result id into a variable
$newspecies = $prelimary_array[$index];
//put that variable in an array
$results_array[] = $newspecies; //there is 7 newspecies/winners at the end, I dont want duplicates
}
return $results_array;
}
现在有简单的方法来解决这个问题。你可以尝试在你的查询中使用group by *从taxonomic_units中选择*其中tsn = $可能的组由tsn。或由其他一些库仑组合。 – pregmatch
改变你的mysql查询来选择不同或分组由 –
使用Mysql的'DISTINCT' –