-1
if(( (in_array(0,$status_arr)) || (in_array(0,$genstatus_arr)) ) && 
    ((!in_array(0,$escalation_arr)) || (!in_array(0,$genescalation_arr))) ){

     echo 'Something';

}else if(( (in_array(1,$status_arr)) || (in_array(1,$genstatus_arr)) )){

    echo 'Something Else';

}

Here I am comparing 0 value in_array and non-zero which is !in_array. The results varies according to if-else condition but does not output expected result.

To avoid this, I tried with the third parameter, true, placing the comparison in strict mode which will not only compare values, but types as well:

How can I check if 0 or 1 exists in some array and do not exist in another array ?

The array values would be like below:

var_dump($status_arr);
   array (size=6)
  0 => string '0' (length=1)
  1 => string '0' (length=1)
  2 => string '0' (length=1)
  3 => string '0' (length=1)
  4 => string '0' (length=1)

var_dump($genstatus_arr);
   array (size=6)
  0 => string '1' (length=1)
  1 => string '1' (length=1)
  2 => string '1' (length=1)
  3 => string '1' (length=1)
  4 => string '1' (length=1)

For the below condition its not working

if( (in_array(0,$status_arr)) && (!in_array(0,$genstatus_arr)) )

Also its not a possible duplicate of IN ARRAY

12
  • edit with your print_r($arr) Commented Apr 3, 2015 at 7:01
  • "Results are abnormal" – what results do you get and what do you expect instead? Commented Apr 3, 2015 at 7:05
  • @AdrianCidAlmaguer, array values updated. Commented Apr 3, 2015 at 7:05
  • the edit that you made is for which array? can you be more specific? Commented Apr 3, 2015 at 7:06
  • Abnormal here states, that if I expect in_array to check value 0 inside an array is not working. Also !in_array Commented Apr 3, 2015 at 7:06

2 Answers 2

0

var_dump(in_array(0, $status_arr, true));

Should return true, nothing abnormal there. If you are seeing false it is most probably because either of your 0's is a string 0 and not numeric 0.

If that is the case, remove the strict parameter

$array=array(0,2,3);
var_dump(in_array(0,$array,true));

Returns

 bool(true)

And

$array=array('0','2','3');
var_dump(in_array(0,$array,true));

Returns

bool(false)

You mentioned

For the below condition its not working

print_r($status_arr);
Array([0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0)

print_r($genstatus_arr);
Array([0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 1 [5] => 1)

if( (in_array(0,$status_arr)) && (!in_array(0,$genstatus_arr)) )

Which is an incorrect observation, because the condition works perfectly fine.

Example

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

3 Comments

var_dump(in_array(0, $genstatus_arr, true)); outputs boolean false. Should I make it in_array('0')
Why do you want that last true parameter in there? Just remove it. That will match a 0 with a 0 but not a 0 with a '0' Since one is a number and other is a string.
var_dump(in_array(0, $genstatus_arr));
0

In your case your are trying to work with strings

var_dump($status_arr);
   array (size=6)
  0 => string '0' (length=1)
  1 => string '0' (length=1)
  2 => string '0' (length=1)
  3 => string '0' (length=1)
  4 => string '0' (length=1)

var_dump($genstatus_arr);
   array (size=6)
  0 => string '1' (length=1)
  1 => string '1' (length=1)
  2 => string '1' (length=1)
  3 => string '1' (length=1)
  4 => string '1' (length=1)

Just try this example:

<?php

$status_arr = array();
$status_arr = array_pad($status_arr, 6, 0);

$genstatus_arr = array();
$genstatus_arr = array_pad($genstatus_arr, 6, 1);

var_dump($status_arr);
var_dump($genstatus_arr);

if( (in_array(0,$status_arr)) && (!in_array(0,$genstatus_arr)) )  {

     echo 'Works as espected';

}

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.