php - Recursivly format object -
i have object in format can infinately deep.
stdclass object ( [condition] => , [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] => , [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 -0500 [1] => 06:00 -0500 ) ) [2] => stdclass object ( [id] => request [field] => request [type] => string [input] => select [operator] => equal [value] => post ) ) ) ) )
the format im trying
array( 0 => array( 'condition' => 'and', 'rules' => array({ruleshere}) ), 1 => array( 'condition' => 'and', 'rules' => array({rules here}) ), );
what have @ moment failing hardcore.
public function loop(obj) { // first condition $this->stack[$this->stackcounter]['condition'] = $obj->condition; $this->stack[$this->stackcounter]['rules'] = array(); foreach ($obj $rule) { if (isset($rule->condition)) { $this->loop($rule); } else { array_push($this->stack[$this->stackcounter]['rules'], $rule); // $this->evalcondition($rule); } } $this->stackcounter++; } }
could please lend idiot hand.
try
foreach ($obj->rules $rule) {
stack counter should passed parameter make local. otherwise stack counter change globally go branch loop , come iterate next rules. code looks this:
public function loop($obj, $stackcounter) { $this->stack[$stackcounter]['condition'] = $obj->condition; $this->stack[$stackcounter]['rules'] = array(); foreach ($obj->rules $rule) { if (isset($rule->condition)) { $this->loop($rule, $stackcounter + 1); } else { array_push($this->stack[$stackcounter]['rules'], $rule); } } }
Comments
Post a Comment