我的数组包含像
这样的值Array
(
[0] = true
[1] = OR
[2] = true
[3] = AND
[4] = false
[5] = AND
[6] = true
)
我想在php中创建真值表,例如
true OR true = result1
result1 AND false = result2
result2 AND true = result3
我很蠢,而且还无法创造。 :(
这是我的代码
foreach( $arrValue as $val )
{
if(!empty($val))
{
if($val=='true')
$p = true;
elseif($val=='false')
$p = false;
if(isset($p))
{
if(isset($result))
{
if($val=='AND')
$result = $result AND $p;
elseif($val=='OR')
$$result = $result or $p;
}
else
$result = $p;
}
}
}
请帮助这个愚蠢的男孩取得成果。
答案 0 :(得分:1)
您是否考虑过使用回调创建真值表,例如:
$table = array();
$table[] = array(
'name' => 'equals',
'call' => 'check_equals'
);
$table[] = array(
'name' => 'or',
'call' => 'check_or'
);
$myData = array(
true => true,
false => true,
true => false,
true => -1,
)
foreach($myData as $first => $second)
{
foreach($table as $check)
{
echo $check['name'] . call_user_func($check['call'],$first,$second) ? 'good' : 'bad';
}
}
然后只需创建你的回调:
function check_or($f1,$f2)
{
return $f1 || $f2;
}
function check_equals($f1,$f2)
{
return $f1 === $f2;
}
答案 1 :(得分:0)
这就是我想要的。
//( [0] => true [1] => OR [2] => true [3] => AND [4] => false [5] => AND [6] => true)
function boolstr($val) {
if($val=='true')
return true;
elseif($val=='false')
return false;
}
function row_operator($arrValue){
$i=-2; $j=-1;
foreach( $arrValue as $val )
{
if($i+3 > count($arrValue)) break;
$boolste = boolstr($arrValue[$i+2]);
if(isset($res))
{
if($arrValue[$j+2]=='AND')
$res = $res && $boolste;
elseif($arrValue[$j+2]=='OR')
$res = $res || $boolste;
if($res==true)
$a='true';
else $a= 'false';
$j += 2;
}
else
$res = $boolste;
$i += 2;
}
return $res;
}