while($row = $update_post->fetch_array()){
//Explodes checkbox values
$res = $row['column_name'];
$field = explode(",", $res);
$arr = array('r1','r2,'r3','r4','r5','r6');
if (in_array($arr, $field)) {
echo "<script>alert('something to do')</script>";
}else{
echo "<script>alert('something to do')</script>";
}
}
如何检查$ arr的值是否等于$ field的值。
先谢谢你。
答案 0 :(得分:4)
如果你想匹配两个数组而不是你需要在这里使用array_intersect()
。
如果您想使用in_array()
检查特定值,则需要在此处使用循环:
<?php
$res = $row['column_name'];
$field = explode(",", $res);
$arr = array('r1','r2','r3','r4','r5','r6');
foreach ($arr as $value) {
if(in_array($value, $field)) {
echo "success";
}
else{
echo "failed";
}
}
?>
According to manual: in_array - 检查数组中是否存在值
另请注意,您的数组中存在语法错误:
$arr = array('r1','r2,'r3','r4','r5','r6'); // missing quote here for r2
<强>更新强>
如果你想使用array_intersect()
,你可以这样检查:
<?php
$arr1 = array('r1','r2');
$arr2 = array('r1','r2','r3','r4','r5','r6');
$result = !empty(array_intersect($arr1, $arr2));
if($result){
echo "true";
}
else{
echo "false";
}
?>
<强> DEMO 强>
更新2:
如果您想使用array_intersect()
检查您获得的值比您可以使用的那样:
<?php
$arr1 = array('r2');
$arr2 = array('r1','r2','r3','r4','r5','r6');
$result = array_intersect($arr1, $arr2);
if(count($result)){
echo "Following ID(s) found: ".implode(",",$result);
}
?>
<强> DEMO 强>
答案 1 :(得分:1)
通过array_intersect比较两个数组,然后检查let selectedOptionId = "957";
let array = [{"957":"1269"},{"958":"1265"},{"956":"1259"},{"957":"1269"},{"947":"1267"}];
let filtered = array.filter(function(element){
return Object.keys(element)[0] === selectedOptionId;
});
console.log(filtered);
以了解匹配数组值有...
array_intersect
比较两个数组的值,并返回匹配项:
console.log(_.some($scope.array, function(o) { return _.has(o, "957"); }));
}