元素在sql的php数组上重复

时间:2013-01-08 11:06:39

标签: java php sql arrays select

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

 $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;

    }
}

1 个答案:

答案 0 :(得分: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($ array)将检测数组中的相同值,并仅提供第一个出现的值,其余被跳过。

已编辑:

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