我想检查将数据输入数据库。检查如下
$implode1 = "cat, dog, chicken";
$implode2 = "cow, goat, cat";
如果变量中的猫$implode1
也包含在变量$implode2
中,则应显示警告消息。如何为上述问题编码?
请帮帮我 :(
您可以explode
将字符串转换为数组,然后用于array_intersect
返回两者中都存在的值,例如:
$string1 = 'cat, dog, chicken';
$string2 = 'cow, goat, cat';
$compare = explode(', ', $string1);
$against = explode(', ', $string2);
$matches = array_intersect($compare, $against);
$implode1 = "cat, dog, chicken";
$implode2 = "cow, goat, cat";
$imp1 = explode(', ',$implode1);
$imp2 = explode(', ',$implode2);
foreach($imp1 as $val){
if(in_array($val,$imp2)) {
echo "$val is present in $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)
您可以在将值插入数据库之前检查它们是否已经存在:
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);
}