我正在使用jquery查询生成器,$ rule是数组 如何以适当的条件遍历所有内部数组并返回$ rule是true还是false?
$rule = array (
'condition' => 'AND',
'rules' => array (
0 => array (
'id' => 'name',
'field' => 'name',
'type' => 'string',
'input' => 'text',
'operator' => 'equal',
'value' => 'bibin',
),
1 => array (
'id' => 'category',
'field' => 'category',
'type' => 'integer',
'input' => 'select',
'operator' => 'not_equal',
'value' => 1,
),
2 => array (
'condition' => 'OR',
'rules' => array (
0 => array (
'id' => 'name',
'field' => 'name',
'type' => 'string',
'input' => 'text',
'operator' => 'equal',
'value' => 'john',
),
1 => array (
'id' => 'category',
'field' => 'category',
'type' => 'integer',
'input' => 'select',
'operator' => 'equal',
'value' => 2,
),
2 => array (
'condition' => 'OR',
'rules' => array (
0 => array (
'id' => 'name',
'field' => 'name',
'type' => 'string',
'input' => 'text',
'operator' => 'equal',
'value' => 'tech',
),
1 => array (
'id' => 'price',
'field' => 'price',
'type' => 'double',
'input' => 'number',
'operator' => 'greater_or_equal',
'value' => 500,
),
),
),
3 => array (
'condition' => 'AND',
'rules' => array (
0 => array (
'id' => 'name',
'field' => 'name',
'type' => 'string',
'input' => 'text',
'operator' => 'equal',
'value' => 'top',
),
1 => array (
'id' => 'category',
'field' => 'category',
'type' => 'integer',
'input' => 'select',
'operator' => 'equal',
'value' => 5,
),
),
),
),
),
3 => array (
'condition' => 'AND',
'rules' => array (
0 => array (
'id' => 'name',
'field' => 'name',
'type' => 'string',
'input' => 'text',
'operator' => 'equal',
'value' => 'vishnu',
),
1 => array (
'id' => 'price',
'field' => 'price',
'type' => 'double',
'input' => 'number',
'operator' => 'less_or_equal',
'value' => 1000,
),
),
),
)
);
我有一个称为$ rule的嵌套数组。 我想检查数组是否返回true或false 有什么办法检查此数组返回true或false吗? 如何遍历并检查内部数组?
答案 0 :(得分:0)
U可以使用foreach循环访问每个嵌套数组:
echo "<pre>";
foreach ($rule as $r) {
var_dump($r);
}
如果您要访问某些属性(键=>值),则可以使用$r['id'];
$r['field'];
等来实现。
答案 1 :(得分:0)
您有一个适用于某些规则的逻辑运算符。
您可能需要一个在不同规则上使用该运算符的函数。
我认为您是用这种方式构建的
/*
* $operator being 'OR' / 'AND'
* $rules being an array
* returns true or false
*/
function ApplyCondition($operator, $rules)
{
// some code
}
我建议您构建一个将遍历规则的函数,并检查$ rules数组是否具有'condition'
键。由于原始数组是一棵树(您不知道嵌套数组的深度),因此您必须使用递归函数对其进行解析。
/*
* $rules being an array
* At first call, you have no condition yet, hence its default value is null
* returns true or false
*/
function ParseRules($rules, $condition = null)
{
if (isset($rules['condition']) && isset($rules['rules']))
{
/*
* An array with some rules and a condition was found.
* Let's do a recursive call that will return true or false
*/
return (ParseRules($rules['rules'], $rules['condition']));
}
else
{
/*
* An array without a condition was found.
* Let's apply the upper function that will return true or false
* to the previous recursive call
*/
return (ApplyCondition($condition, $rules));
}
}
/*
* $operator being 'OR' / 'AND'
* $rules being an array
* returns true or false
*/
function ApplyCondition($operator, $rules)
{
foreach ($rules as $rule)
{
if (isset($rule['condition']) && isset($rule['rules']))
{
/*
* An array with some rules and a condition was found.
* Let's do a recursive call that will return true or false
*/
$SomeBooleanValueToHandle = ParseRules($rule['rules'], $rule['condition']);
}
else
{
// some code
}
}
return ($SomeBooleanValueToHandle);
}