Php 使用数组Intersect检查数据的编码
我想通过检查将数据输入数据库Php 使用数组Intersect检查数据的编码,php,arrays,Php,Arrays,我想通过检查将数据输入数据库 $implode1 = "cat, dog, chicken"; $implode2 = "cow, goat, cat"; 如果变量$inpulode1中的cat也包含在变量$inpulode2中,则应显示一条警告消息。如何为上述问题编写代码 请帮助我:(循环第一个数组并检查第二个数组中是否存在任何元素-类似以下内容: foreach($implode1 as $val){ if(in_array($val,$implode2)) { echo "$
$implode1 = "cat, dog, chicken";
$implode2 = "cow, goat, cat";
如果变量$inpulode1
中的cat也包含在变量$inpulode2
中,则应显示一条警告消息。如何为上述问题编写代码
请帮助我:(循环第一个数组并检查第二个数组中是否存在任何元素-类似以下内容:
foreach($implode1 as $val){
if(in_array($val,$implode2)) {
echo "$val is exists in the implode2 array";
}
}
哦,对不起,这些只是字符串。首先将它们爆炸:
arr_implode1 = explode(", ",$implode1)
arr_implode1 = explode(", ",$implode2)
循环第一个数组并检查第二个数组中是否存在任何元素-类似以下内容:
foreach($implode1 as $val){
if(in_array($val,$implode2)) {
echo "$val is exists in the implode2 array";
}
}
哦,对不起,这些只是字符串。首先将它们爆炸:
arr_implode1 = explode(", ",$implode1)
arr_implode1 = explode(", ",$implode2)
您可以将字符串转换为数组,然后使用返回两个数组中的值,例如:
$string1 = 'cat, dog, chicken';
$string2 = 'cow, goat, cat';
$compare = explode(', ', $string1);
$against = explode(', ', $string2);
$matches = array_intersect($compare, $against);
您可以将字符串转换为数组,然后使用返回两个数组中的值,例如:
$string1 = 'cat, dog, chicken';
$string2 = 'cow, goat, cat';
$compare = explode(', ', $string1);
$against = explode(', ', $string2);
$matches = array_intersect($compare, $against);
如果值已经存在,则可以在将其插入数据库之前进行检查:
if not exists (select * from TestTable where column NOT IN {$implode})
begin
...Do something here!!
end
如果值已经存在,则可以在将其插入数据库之前进行检查:
if not exists (select * from TestTable where column NOT IN {$implode})
begin
...Do something here!!
end
自己创建一个函数,该函数能够以数组的形式从每个字符串中提取值,然后获取交叉点。如果该函数不为FALSE,则表示存在交叉点,请执行以下警告:
$values = function($string) {
return explode(', ', $string);
};
if (array_intersect($values($implode1), $values($implode2))) {
trigger_error('Values intersect', E_USER_WARNING);
}
.自己创建一个函数,该函数能够以数组的形式从每个字符串中提取值,然后获取交叉点。如果它不为FALSE,则存在交叉点,因此请执行警告:
$values = function($string) {
return explode(', ', $string);
};
if (array_intersect($values($implode1), $values($implode2))) {
trigger_error('Values intersect', E_USER_WARNING);
}
.所以您只想输入唯一的项目?更好的方法是截断字符串的所有出现处,除了一个,而不是抛出警告。所以您只想输入唯一的项目?更好的方法是截断字符串的所有出现处,除了一个,而不是抛出警告。