2013-01-08 73 views
0

我有两个数组:在一个我插入所有的问题ID从我的SELECT和另一个数组我想要插入相同的ID的但没有重复这次。我在第二个数组中的代码不起作用,我不知道为什么。我不能在我的SELECT中使用DISTINCT,因为不起作用(行是diferents),我不想为此使用两个选择。在sql数组上重复的元素

$query_slidersanswers= "SELECT A.QuestionIDFK, A.AnswerIDPK, A.AnswerValue, A.SortOrder 
         FROM tblquestionset AS QS 
          INNER JOIN tblquestion AS Q ON QS.QuestionIDFKPK = Q.QuestionIDPK 
          INNER JOIN tblanswer AS A ON Q.QuestionIDPK = A.QuestionIDFK 
         WHERE QS.QuestionSetIDPK = '0' 
          AND QS.OnPage = '1' 
          AND Q.Constructor = '".$_session['slider']."'"; 


$Query_Sliders= mysql_query($query_slidersanswers);   

$currentQuestionID= 0; 
while($row_Slider=mysql_fetch_array($Query_Sliders)){ 

    $QuestionID=$row_Slider['QuestionIDFK'] ; 
    $AnswerID=$row_Slider['AnswerIDPK'] ; 
    $AnswerValue=$row_Slider['AnswerValue'] ; 
    $SortOrder=$row_Slider['SortOrder'] ; 


    $tableslidersqid[] = array($QuestionID); 

    if($QuestionID != $currentQuestionID){ 
    //I DO THIS FOR OBTAIN other array with THE UNIQUES ID'S (non repeated) 

    $tableslidersREALqid[] = array($QuestionID); 
    $CurrentQuestionID = $QuestionID; 

    } 
} 
+0

你可以尝试GROUP BY A.QuestionIDFK –

+0

我不想改变选择,我需要两个数组。 – user1901142

回答

0

假设您的阵列,用于问题id是下面

$input = array("19", "55", "19", "55", "78"); 
$result = array_unique($input); 
print_r($result); 

上例将输出:

Array 
(
    [0] => 19 
    [1] => 55 
    [4] => 78 
) 

array_unique($阵列)将检测在相同的值数组并且只给出第一个发生的值,其余的被跳过。

编辑:

while($row_Slider=mysql_fetch_array($Query_Sliders)){ 

    $QuestionID[]=$row_Slider['QuestionIDFK']; //used $QuestionID[], instead of $QuestionID 

    //$AnswerID=$row_Slider['AnswerIDPK'] ; 
    //$AnswerValue=$row_Slider['AnswerValue'] ; 
    //$SortOrder=$row_Slider['SortOrder'] ; 


    //$tableslidersqid[] = array($QuestionID); 

    //if($QuestionID != $currentQuestionID){ 
    //$tableslidersREALqid[] = array($QuestionID); 
    //$CurrentQuestionID = $QuestionID; 
    //} 
} 

$result = array_unique($QuestionID); //make array unique here, after all the ids are in the array 
+0

我喜欢你的想法,但我不没有为什么不工作,如果我把$ tableslidersREALqid [] = array_unique($ tableslidersqid);出来的时候只插入第一个,如果我把$ input = array($ QuestionID);在while和after(out)之内:tableslidersREALqid [] = array_unique($ input);不工作,只插入last.why? – user1901142

+0

我已经使用您的代码编辑了我的代码。我用$ QuestionID []。 –

+0

我见过它。现在我明白了为什么不用我的代码工作。谢谢! – user1901142