[英]How to compare two array in php
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>";
}
}
How to check if value of $arr is equal to the value of $field. 如何检查$ arr的值是否等于$ field的值。
Thank you in Advance. 先感谢您。
If you want to match two array than you need to use array_intersect()
here. 如果你想匹配两个数组而不是你需要在这里使用array_intersect()
。
If you want to check specific value with in_array()
than you need to use loop here as: 如果要使用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 — Checks if a value exists in an array 根据手册: in_array - 检查数组中是否存在值
Also note that, you have a syntax error in your array: 另请注意,您的数组中存在语法错误:
$arr = array('r1','r2,'r3','r4','r5','r6'); // missing quote here for r2
Update: 更新:
If you want to use array_intersect()
than you can check like that: 如果你想使用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";
}
?>
Update 2: 更新2:
If you want to check which value are you getting by using array_intersect()
than you can use like: 如果你想通过使用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);
}
?>
compare two array by array_intersect then check with count
to know matches array value has... 通过array_intersect比较两个数组然后用count
来检查知道匹配数组值有...
array_intersect array_intersect
Compare the values of two arrays, and return the matches: 比较两个数组的值,并返回匹配项:
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 (count(array_intersect($arr, $field)) > 0) {
echo "<script>alert('duplicate array')</script>";
}else{
echo "<script>alert('something to do')</script>";
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.