douyi4991 2014-11-11 16:04
浏览 27
已采纳

递归格式化对象

i have an object in this format that can be infinately deep.

stdClass Object
(
    [condition] => AND
    [rules] => Array
        (
            [0] => stdClass Object
                (
                    [id] => app
                    [field] => app
                    [type] => string
                    [operator] => equal
                    [value] => files_sharing
                )

            [1] => stdClass Object
                (
                    [id] => deviceType
                    [field] => deviceType
                    [type] => string
                    [input] => select
                    [operator] => equal
                    [value] => android
                )

            [2] => stdClass Object
                (
                    [id] => userGroup
                    [field] => userGroup
                    [type] => string
                    [operator] => equal
                    [value] => admin
                )

            [3] => stdClass Object
                (
                    [condition] => AND
                    [rules] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => userGroup
                                    [field] => userGroup
                                    [type] => string
                                    [operator] => equal
                                    [value] => group
                                )

                            [1] => stdClass Object
                                (
                                    [id] => time
                                    [field] => time
                                    [type] => time
                                    [input] => text
                                    [operator] => between
                                    [value] => Array
                                        (
                                            [0] => 12:30 am -0500
                                            [1] => 06:00 am -0500
                                        )

                                )

                            [2] => stdClass Object
                                (
                                    [id] => request
                                    [field] => request
                                    [type] => string
                                    [input] => select
                                    [operator] => equal
                                    [value] => post
                                )

                        )

                )

        )

)

The format im trying to get it into is

array(
0 => array(
'condition' => 'and',
'rules' => array({RULESHERE})
),
1 => array(
'condition' => 'and',
'rules' => array({RULES HERE})
),
);

What i have at the moment is this but it is failing hardcore.

Public function loop(obj) {
    // First we get the condition
            $this->stack[$this->stackCounter]['condition'] = $obj->condition;
            $this->stack[$this->stackCounter]['rules'] = array();

            foreach ($obj as $rule) {
                if (isset($rule->condition)) {
                    $this->loop($rule);
                } else {
                    array_push($this->stack[$this->stackCounter]['rules'], $rule);
    //                      $this->evalCondition($rule);
                }
            }
            $this->stackCounter++;
        }
}

Could someone please lend an idiot a hand.

  • 写回答

1条回答 默认 最新

  • dpepbjp126917 2014-11-11 16:13
    关注

    Try

    foreach ($obj->rules as $rule) {
    

    Stack counter should be passed as parameter to make it local. Otherwise the stack counter will change globally as soon as you go into a branch loop and come back to iterate the next rules. So the code looks like this:

    public function loop($obj, $stackCounter)
    {
        $this->stack[$stackCounter]['condition'] = $obj->condition;
        $this->stack[$stackCounter]['rules'] = array();
    
        foreach ($obj->rules as $rule) {
            if (isset($rule->condition)) {
                $this->loop($rule, $stackCounter + 1);
            } else {
                array_push($this->stack[$stackCounter]['rules'], $rule);
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败