我有以下PHP代码,它根据定义的标准从同一个表中的各个列获取数据。
$arraySecondaryNames = array();
$sqlQuerySecondaryCat01 = $dbh->prepare("SELECT secondary_category_set_1 FROM table_name WHERE primary_category_set_1 = :primary_cat");
$sqlQuerySecondaryCat01->execute(array(':primary_cat'=>$primaryCatText, ':profile_switch'=>$profileSwitchOn, ':profile_visibility'=>$profileVisShow));
foreach($sqlQuerySecondaryCat01 as $sqlResultSecondaryCat01){
$arraySecondaryNames[] = $sqlResultSecondaryCat01['secondary_category_set_1'];
}
$sqlQuerySecondaryCat02 = $dbh->prepare("SELECT secondary_category_set_2 FROM table_name WHERE primary_category_set_2 = :primary_cat");
$sqlQuerySecondaryCat02->execute(array(':primary_cat'=>$primaryCatText, ':profile_switch'=>$profileSwitchOn, ':profile_visibility'=>$profileVisShow));
foreach($sqlQuerySecondaryCat02 as $sqlResultSecondaryCat02){
$arraySecondaryNames[] = $sqlResultSecondaryCat02['secondary_category_set_2'];
}
输出有时是重复的:因此我使用以下
$arraySecondaryCatNames = array_filter($arraySecondaryCatNames);
$arraySecondaryCatNames = array_unique($arraySecondaryCatNames);
输出仍包含重复项:
Array ( [0] => Bride and Groom [1] => Bride and Groom [2] => Bride and Groom )
我不确定如何消除重复项。 [0],[1]和[3]的值是相同的,因为array_unique使用区分大小写。
如何消除重复?
答案 0 :(得分:0)
请试试这个
$arraySecondaryNames = array();
$arraySecondaryNames1 = array();
$arraySecondaryNames2 = array();
$sqlQuerySecondaryCat01 = $dbh->prepare("SELECT secondary_category_set_1 FROM table_name WHERE primary_category_set_1 = :primary_cat");
$sqlQuerySecondaryCat01->execute(array(':primary_cat'=>$primaryCatText, ':profile_switch'=>$profileSwitchOn, ':profile_visibility'=>$profileVisShow));
foreach($sqlQuerySecondaryCat01 as $sqlResultSecondaryCat01){
$arraySecondaryNames1[] = $sqlResultSecondaryCat01['secondary_category_set_1']; // First array
}
$sqlQuerySecondaryCat02 = $dbh->prepare("SELECT secondary_category_set_2 FROM table_name WHERE primary_category_set_2 = :primary_cat");
$sqlQuerySecondaryCat02->execute(array(':primary_cat'=>$primaryCatText, ':profile_switch'=>$profileSwitchOn, ':profile_visibility'=>$profileVisShow));
foreach($sqlQuerySecondaryCat02 as $sqlResultSecondaryCat02){
$arraySecondaryNames2[] = $sqlResultSecondaryCat02['secondary_category_set_2']; //Second array
}
$arraySecondaryCatNames = array_unique( array_merge($arraySecondaryNames1,$arraySecondaryNames2) );
//Merged array with no duplicate values
print_r($arraySecondaryCatNames);
答案 1 :(得分:0)
试试这个:
$arraySecondaryCatNames_filter = $arraySecondaryCatNames_unique = array();
$arraySecondaryCatNames_filter = array_filter($arraySecondaryCatNames);
$arraySecondaryCatNames_unique = array_unique($arraySecondaryCatNames_filter);
print_r($arraySecondaryCatNames_unique);