0

我有两个数组:在一个数组中,我从我的 SELECT 中插入了所有问题 ID,在另一个数组中,我想插入相同的 ID,但这次不重复。我在第二个数组中的代码不起作用,我不知道为什么。我不能在我的 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;

    }
}
4

1 回答 1

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
于 2013-01-08T11:23:10.633 回答