2011-10-20 35 views
1

即时创建一个学生主题保留,将添加表中的主题行。我想检测重复值的存在,然后回显如果检测到值。如何检测会话数组php中的重复值?

例如,给定以下的数组:

$c=0; 

     while($c<=$counter){ 
      if($_SESSION["S['$c']"]==""){ 
      echo "-"; 
      } 
     $array1=array($_SESSION["lect['$c']"],$_SESSION["labt['$c']"],$_SESSION["Day['$c']"] ,$_SESSION["lday['$c']"]); 
     if(count(array_unique($array1))<count($array1)){ 
      echo "Duplicate entry found in array"; 
     } 
     else{ 
        echo "<tr><td>".$_SESSION["S['$c']"] . " </td> 
       <td>".$_SESSION["lec['$c']"] . "<br/>".$_SESSION["lab['$c']"] . "</td> 
       <td>".$_SESSION["lect['$c']"] . "<br/>".$_SESSION["labt['$c']"] . "</td> 
       <td>".$_SESSION["Day['$c']"] . "<br/>".$_SESSION["lday['$c']"] . "</td> 
       <td>".$_SESSION["room['$c']"] . "<br/>".$_SESSION["lroom['$c']"] . "</td></tr><br>"; 
      $unit=$_SESSION["lec['$c']"] + $_SESSION["lab['$c']"]; 
       $units= $units+$unit; 
     } 

但是当我添加相同的值的值将重新添加,但是当我添加另一个它显示回声“复制阵列中找到的条目”;我哪里错了?对不起,事先我英语不好的感谢,如果我的代码是错了,请给我一些建议

这里点击事件以及添加到另一个表:

$i=0; 
$b =1; 
while($row=mysql_fetch_assoc($sql)){ 


echo "<tr><td>".$row['Subject']."<input type='hidden' name='subj[$i]' value=".$row['Subject']."></td> 
<td>".$row['Lec']."<br/>".$row['Lab']."<input type='hidden' name='leclab[$i]' value=".$row['Lec']."><input type='hidden' name='lab[$i]' value=".$row['Lab']."></td> 

<td>".$row['Descriptive']."<input type='hidden' name='desc[$i]' value=".$row['Descriptive']."></td> 
<td>".$row['Day']."<br/>".$row['Labday']."<input type='hidden' name='daylabday[$i]' value=".$row['Day']."><input type='hidden' name='labday[$i]' value=".$row['Labday']."></td> 

<td>".$row['LecTime']."<br/>".$row['LabTime']."<input type='hidden' name='lectlabt[$i]' value=".$row['LecTime']."><input type='hidden' name='labtime[$i]' value=".$row['LabTime']."></td> 

<td>".$row['Room']."<br/>".$row['Labroom']."<input type='hidden' name='roomlabroom[$i]' value=".$row['Room']."><input type='hidden' name='labroom[$i]' value=".$row['Labroom']."></td> 

<td><input id='send' name='reserv[$i]' type='submit' value='Add' onclick='addRow('myTable')' /> </td></tr>"; 

$i++; 


} 
} 

下面是输出的样本,但所有我想要的是在表格上添加相同的值来回显错误,我可以如何防止它?

enter image description here

+3

你是在滥用数组键表示法。你应该使用'$ _SESSION ['lect'] [$ c]'来创建适当的子数组,而不是将数组下标嵌入到键名中。 –

回答

0

你可以混合使用array_uniquearray_diff首先清理重复,然后作出一个diff得到只含重复值

+0

你能解释更多先生我真的不明白cuz即时通讯新的php – nobito

0

array_count_values()将计重复的值数组。那么你可以filter out那些计数1,看看哪些是重复的

+0

你能解释更多先生我不能真正了解cuz即时新的php – nobito

+0

你可以添加一些示例输入和输出为您的代码?我不确定你所要求的是你需要什么,重新阅读你的代码 – Einacio

+0

后,如果我将value1添加到表中并重新添加它,它会回显相同的值,我不能发布样例图片。 – nobito