- &GT; FetchRow();$ newspecies = $ rown [’ TSN </跨度> “];$ result_array [] = $ newspecies;
for($ i = 1; $ i&lt; count($ array_result); $ i ++){ $ previous = implode(‘,’,$ result_array);$ queryn =“select taxonomic_units.tsn taxonomic_units.tsn not in(‘$ previous’) 按rand()排序 限制1“;$ resultn = $ dbn - &gt;执行($ queryn);$ rown = $ resultn-&GT; FetchRow();$ newspecies = $ rown [’ TSN </跨度> “];$ result_array [] = $ newspecies;}
你的$ array_result结构是什么?
array(''=&gt;'');
要么
array(array(''=&gt;''));
如果您的阵列限制是第1,那么您可以使用 array_unique 检查重复的ID。
如果你的数组结构是第二个,你可能需要在运行查询之前做一些过滤功能。
你可以这样做:
$previous = implode(',', $result_array); $queryn = "select taxonomic_units.tsn, hierarchy.hierarchy_string, hierarchy.TSN, taxonomic_units.rank_id from hierarchy left join taxonomic_units on hierarchy.TSN = taxonomic_units.tsn where taxonomic_units.rank_id = 220 and hierarchy.hierarchy_string LIKE '%$id%'order and taxonomic_units.tsn not in (".$previous.") order by rand() limit 1";
注意决赛 and 不包括 tsn 你已经检索过了。它可能需要一点调整(检查是否 $result_array 是空的,等等,但这是基本的想法。
and
tsn
$result_array
您可以在存储该ID之前搜索重复项。使用 in_array 只是。
in_array
function getrandomspecies($array_result){ ... $i=0; //If your $array_result begin with 0 while($i<count($array_result)) { ... $newspecies = $rown['tsn']; if(in_array($newspecies, $result_array))) continue; else { $result_array[] = $newspecies; $i++; } } }