繁体   English   中英

如何检查PHP嵌套数组条件

[英]how to check php nested array conditions

我正在使用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? 如何遍历并检查内部数组?

U可以使用foreach循环访问每个嵌套数组:

echo "<pre>";
foreach ($rule as $r) {
    var_dump($r);
}

如果您要访问某些属性(键=>值),则可以使用$r['id']; $r['field']; ..等

您有适用于某些规则的逻辑运算符。

您可能需要一个在不同规则上使用该运算符的函数。

我想你是这样建造的

/*
 * $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);
}

暂无
暂无

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

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