【发布时间】:2014-07-07 07:06:30
【问题描述】:
我有两个数组。在一个数组中,我存储一张表中的数据。我将另一个表中的数据存储在另一个数组中。我想比较两个数组的数据。如果第一个数组的数据在第二个数组中,我想继续。我该怎么做?
我尝试了以下代码,但它不是工作事件,尽管 array1 数字存在于 array2 中:
$x = "SELECT * FROM table1";
$data1 = mysqli_query($link, $x);
$dat1 = array()
while($row1= mysqli_fetch_array($data,MYSQLI_ASSOC))
{
$dat1[] = $row1;
$f1 = $row1['fid'];
}
$y = "SELECT * FROM table2";
$data2 = mysqli_query($link, $y);
$dat2 = array()
while($row2= mysqli_fetch_array($data2,MYSQLI_ASSOC))
{
$dat2[] = $row2;
$f2 = $row2['fid'];
}
if(in_array($dat1,$dat2))
{
// if exists proceed
}
else
{
// if not show error
}
【问题讨论】:
-
请提供正反两例。请注意,您在代码中将
$dat2拼错为$dta2。 -
提问者,
if(in_array($data,$dta2))中的$dta2是错字吗? -
哈哈,这只是一个错字...当我检查
if(atleast one of data2 matches with data1) { //procesed }else { //show error}时让 data1 = 1、2,3 和 data2= 1 -
@Thauwa 绝对是错字。我修好了。