4
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 a value of $arr is equal to the value of $field.

2
  • 1
    u need to use array_intersect, in_array will not work here Commented Nov 9, 2016 at 7:39
  • check the response in chat... Commented Nov 9, 2016 at 9:47

2 Answers 2

4

If you want to match two array than you need to use array_intersect() here.

If you want to check specific value with in_array() than you need to use loop here as:

<?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

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:

<?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

Update 2:

If you want to check which value are you getting by using array_intersect() than you can use like:

<?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

Sign up to request clarification or add additional context in comments.

9 Comments

What if the value of $field is 'r1','r2','r3' only. is array_intersect return true?
@oo7: no array_intersect only return the matched array
is their anyway on how to know if $field is equal to any index of $arr?
let's assume that $field = r2,r3. then how can I identify than r2,r3 are location to the $arr?
@oo7: yes friend, you can use in_array
|
1

compare two array by array_intersect then check with count to know matches array value has...

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>";
        }

}

3 Comments

if $field return a value of r2 then compare if $arr had a value of r2. then return true. How could I do that?
it is not return true , it return match array .Let say if $field has value of r2 ,then compare with $arr had a value of r2 ,if it has it will return that matches array !!! So count that return array has more than 0 it means it has matches array
in that case can we identify the index location of the matched in $arr?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.