慕标琳琳
我不知道它是否适用于所有情况,但在您的情况下效果很好(我猜)我建议你删除嵌套数组$condition[1]并直接写入$condition[1][0]数组(新的数组结构在下面的代码中)<?php$condition = array( 'and', array( 'field' => 'show_image', 'compare' => '==', 'value' => 1 ), array( 'or', array( 'field' => 'filter', 'compare' => '!=', 'value' => 1 ), array( 'field' => 'align', 'compare' => '==', 'value' => 'left' ) ));function generateExpression( Array $condition ) : String{ return $condition['field'] . " " . $condition['compare'] . " " . $condition['value'];}function generateConditionExpression ( Array $conditionTree ) : String{ if ( array_key_exists("field",$conditionTree) ) { return generateExpression($conditionTree); } //else return "(" . generateConditionExpression( $conditionTree[1] ) . " " . $conditionTree[0] . " " . generateConditionExpression( $conditionTree[2] ) . ")";}echo generateConditionExpression($condition);