dongqin1861 2018-08-30 07:43
浏览 8
已采纳

如何检查php嵌套数组条件

i am using jquery query builder , $rule is array how to loop thround all inner arrays with their proper condition and returns the $rule is true or 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,
                 ),
             ),
         ),
     )
);

I have a nested array called $rule. I want to check the array returns true or false Is any idea to check this array returns true or false? How to iterarte and check with inner arrays?

  • 写回答

2条回答 默认 最新

  • dongyou7739 2018-08-30 08:16
    关注

    You have a logical operator applying to some rules.

    You might need a function that uses that operator on the differents rules.

    I assume you build it that way

    /*
     * $operator being 'OR' / 'AND'
     * $rules being an array
     * returns true or false
     */
    function ApplyCondition($operator, $rules)
    {
        // some code
    }
    

    I suggest you to build a function that will loop through the rules and check if the $rules array has a 'condition' key. Since your original array is a tree (you don't know how deep can be the nested arrays), you must use a recursive function to parse it.

    /*
     * $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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像