简体   繁体   English

在PHP中的数组元素之间应用逻辑运算符

[英]Apply logical operator between array elements in PHP

I have a three array , one is the boolean array which contains all boolean values, second is the conditional array which contains and/or operations between the array of boolean and the third one is the route page array which if the expression is true then it is redirect to next route page else not eg array 我有一个三个array ,一个是包含所有布尔值的布尔数组,第二个是包含和/或布尔数组之间的操作的条件数组,第三个是路由页数组,如果表达式为真,那么它重定向到下一个路由页面,否则不是例如数组

array(2) { [0]=> bool(true) [1]=> bool(false) ...} // boolean array
array(2) { [0]=> string(2) "or" [1]=> string(0) "" ...} // conditional operator array
array(2) { [0]=> string(1) "2" [1]=> string(1) "2" ... } // route page 

the expression would be made (bool(true) or bool(false)) hence the result would be bool(true) final and hence it would redirect to page two ie What I need the recursive expression in loop 表达式将被生成(bool(true) or bool(false))因此结果将是bool(true) final,因此它将重定向到第2页,即我需要循环中的递归表达式

Here is the code what I applied 这是我应用的代码

 for($i=0;$i<count($boolean);$i++)
           {
               if($and_or[$i]=='and')
               {
                   $operator='&&';
               }
               else
               {
                   $operator='||';
               }
               $result=($boolean[$i].$operator.$boolean[$i+1]);

           }
           var_dump($result);

As I didn't like the complexity of the suggested answers I dug deeper and ended up scanning the array functions listed in http://php.net/manual/en/ref.array.php . 由于我不喜欢建议答案的复杂性,我深入挖掘并最终扫描了http://php.net/manual/en/ref.array.php中列出的数组函数。

These might not be the best performance-wise, but are simple and I wouldn't consider the performance penalty if you're not applying on a large array. 这些可能不是性能最好的,但很简单,如果你不申请大型阵列,我不会考虑性能损失。

For logical AND ( && ) use this: 对于逻辑AND&& )使用此:

var_export(!in_array(false,array(true,true,false,false,true,false))); //false

or this: 或这个:

var_export((bool)array_product(array(true,true,false,false,true,false))); //false

For logical OR ( || ) use this: 对于逻辑OR|| )使用此:

var_export(in_array(true,array(true,true,false,false,true,false))); //true

or this: 或这个:

var_export(array_search(true,array(true,true,false,false,true,false))!==false); //true

I am taking a wild guess the in_array 's would be faster. 我猜测in_array的速度会更快。 But with PHP 7 out who knows... 但是PHP 7出来谁知道......

Maybe this brings you closer to your goal: 也许这会让你更接近你的目标:

 for($i=0;$i<count($boolean)-1;$i++)
 {
   if ($i < count($and_or) && !empty($and_or[$i]) {
     if($and_or[$i]=='and')
     {
           $operator='&&';
           $result = $boolean[$i] && $boolean[$i+1];
     }
     else
     {
          $operator='||';
          $result = $boolean[$i] || $boolean[$i+1];
     }
     $operation=($boolean[$i].$operator.$boolean[$i+1]);
 }
 var_dump($operation);
 var_dump($result);

Please note that you use most boolean values two times (the second value is the second operator for the first operation and the first operator for the second operation). 请注意,您使用大多数布尔值两次(第二个值是第一个操作的第二个操作符,第二个操作的第一个操作符)。

Try below code: 试试以下代码:

for( $i = 0; $i < count( $boolean ); $i++ ) {
    switch( $and_or[ $i ] ) {
        case '&&':
        case 'and':
            $result = $boolean[ $i ] && $boolean[ $i + 1 ];
            break;
        case '||':
        case 'or':
            $result = $boolean[ $i ] || $boolean[ $i + 1 ];
            break;
    }
    var_dump( $result );
}

You can also use and , or operators. 您也可以使用andor运算符。 If you have more than two operators, you can use eval function like this: 如果您有两个以上的运算符,则可以使用如下的eval函数:

$boolean = [1,0,0];
$o = ['&&','or'];
echo $r = "return ($boolean[0] $o[0] $boolean[1]) $o[1] $boolean[2];";
var_dump( eval($r) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM