0

I have the following array values in $taxIdarr array

Array ( [0] => 1 [1] => 2 [2] => 1 )

How can i check the value 1,2 in in_array I am using the following code

if(in_array("1,2",$taxIdarr))
{
   echo "test";
}

But not working.Please help me

1
  • please clarify your question, do you want to check that both 1 and 2 are contained? Commented Oct 7, 2016 at 6:40

4 Answers 4

2

You have to use array_intersect function. Please check following ans: Your array is:

$taxIdarr = array('1', '2', '1');

now create one haystack array for which you want to check if it is in array or not. i.e. 1 & 2

$haystack = array('1', '2');

Now check using array_intersect

if(count(array_intersect($haystack, $taxIdarr)) > 0){
     // at least one of $taxIdarr is in $haystack
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think you want to check if both 1 and 2 are present in the array. In that case, try this:

if (in_array("1",$taxIdarr) && in_array("1",$taxIdarr)) {
  echo "test";
}

Comments

0

I am changing my code like this its working fine.

if(in_array("1",$taxIdarr) && !in_array("2",$taxIdarr))
 {
    echo "test1";
 }

 if(in_array("2",$taxIdarr) && !in_array("1",$taxIdarr))
 {
    echo "test2";
 }
  if(in_array("1",$taxIdarr) && in_array("2",$taxIdarr))
 {
    echo "test3";
 }

Comments

0
$array= Array ( "1","2","1","3" );
$target = array('1', '2');

if(count(array_intersect( array_unique($array), $target)) == count($target)){
echo "all of is in array";
}else{
echo "all of is not in array";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.