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条)

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示