-4

I have array look like this.

$setLogic = array(array("Conj"=>null,"Topic"=>True),array("Conj"=>"Or","Topic"=>True),array("Conj"=>"Or","Topic"=>false),

I try to build a IF statement with the dynamicly with this way.

foreach($setLogic as $value){
   echo $value['Conj'].(int)$value['Topic'];

    if($value['Conj'].(int)$value['Topic'] == true){
         $getBoolean[] = true;
    }
    else{
         $getBoolean[] = false;
    }

I just need something like this.

(true or true or false) and return true or false

BBBBeloved
  • 75
  • 1
  • 1
  • 7
  • I don't see any `if` statement here. – u_mulder Mar 29 '19 at 08:10
  • Where is your if condition? – lifeisbeautiful Mar 29 '19 at 08:11
  • 2
    You want a sort of custom expression engine which evaluates what’s in the array structure. Not exactly the best approach with this array. Perhaps you want to look at existing expression engines like Symfony’s: https://symfony.com/doc/current/components/expression_language. – deceze Mar 29 '19 at 08:13

2 Answers2

-1

I found my answer with my self. It's look like this.

$abc= '';
foreach($setLogic2 as $key => $value){
    $abc.= $value['Conj'].' '.(int)$value['Topic'].' ';
}

//$abc = "0 And 1 And 1";

if(eval("return $abc;") == true) {
    echo 'true boolean';
}
else if(eval("return $abc;") == false){
    echo 'false boolean';
}
else{
    echo 'none';
}
Andreas
  • 2,455
  • 10
  • 21
  • 24
BBBBeloved
  • 75
  • 1
  • 1
  • 7
  • 1
    Please [don't use `eval`](https://stackoverflow.com/q/951373/476) unless you're really really really sure what you're doing… – deceze Mar 29 '19 at 09:19
-2

Ok i will re formulate my answer....

You want to dynamically evaluate an array which contains for each subarray * a verb : "and" or "or" * a value "true or "false"

Quicker and most dirty way is to use eval(). * build your conditions as a string (replace "and" by "&&" and "or" by "||"

'true || true && false'
  • then put the result in a variable and evaluate
eval('$result = (true || true && false);');
var_dump($result);

This works for sure...

elfif
  • 1,837
  • 17
  • 23
  • What exactly is `$value['Conj'].(int)$value['Topic']` supposed to represent? – Jeto Mar 29 '19 at 08:38
  • I try to with new array $setLogic3 = array( array("Conj"=>null, "Topic"=>false), array("Conj"=>"And", "Topic"=>True), array("Conj"=>"And", "Topic"=>True), ) the answer must be false. – BBBBeloved Mar 29 '19 at 08:46