0

I have and array, and I want to convert it into an if conditional.

$array = ['check 1', 'check 2', 'check 3'];

I want the final result to work like this:

if ($var == 'check 1' || $var == 'check 2' || $var == 'check 3') {
    // Do here.
}

I don't have any inspiration, I already searched for convert array to if condition but I couldn't find anything.

How can I convert the array into an if conditional?

Ethan
  • 4,295
  • 4
  • 25
  • 44
Opsional
  • 543
  • 4
  • 14
  • 3
    `in_array` can be useful to you – urfusion Dec 07 '17 at 10:59
  • but, how to check multiple value if i use `in_array` – Opsional Dec 07 '17 at 11:00
  • Loop array. Check value of current iteration of the loop. – ProEvilz Dec 07 '17 at 11:00
  • 3
    `if (in_array($var, $array) || in_array($var1, $array)){ //do something }` – urfusion Dec 07 '17 at 11:02
  • Where is the [array](http://php.net/manual/en/language.types.array.php)? I cannot see any [array](http://php.net/manual/en/function.array.php) in the code you posted. – axiac Dec 07 '17 at 11:17
  • @axiac `$array = ( 'check 1', 'check 2', 'check 3' );` ? then the code below is just an example wit normal var, but he wud like to achieve same using an array – Masivuye Cokile Dec 07 '17 at 11:22
  • @MasivuyeCokile `$array` in this code is not an [array](http://php.net/manual/en/language.types.array.php). The code doesn't even compile, it reports a parsing error at the first comma. So, I'm asking again, where is the [array](http://php.net/manual/en/function.array.php)? – axiac Dec 07 '17 at 11:45
  • A relevant/inspirational reference: https://stackoverflow.com/questions/13483219/what-is-faster-in-array-or-isset – mickmackusa Dec 07 '17 at 14:18

3 Answers3

4

Instead of multiple || (OR) conditions use in_array()

if (in_array($var, $array)) {
    // do here
}

Output:-https://eval.in/914346

If you want to check multiple variables against array then do like this:-

if (in_array($var, $array) || in_array($var1, $array)){ 
  //do here
}

Output:- https://eval.in/914351

Note:- If you can clarify more about all your variables and what conditions you want for all those variables, you will get more accurate answer

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
1

Loop through array and check in_array like this:

$array = ( 'check 1', 'check 2', 'check 3' );

    foreach($array as $k => $v)
    {
        $flag = "false";
        if(in_array($v,$array))
        {
            $flag = "true";
        }
    }

So from above code, is any of the value exist in array, the variable $flag will have string value "true".

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33
1
<?php

$array = array( "mango", "grapes","apple");
//let you have to find apple

if(in_array('masngo',$array) || in_array('mango',$array)){
    echo "FIND";
    //OR WHATEVER YOU WANT TO DO
}else{
    echo "Not Found";
}

Try to run HERE

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
TarangP
  • 2,711
  • 5
  • 20
  • 41