首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PHP RecursiveIterator遍历

PHP RecursiveIterator遍历
EN

Stack Overflow用户
提问于 2010-11-12 20:30:57
回答 2查看 4K关注 0票数 8

我有一个表示表单的结构,我想使用RecursiveIterator对其进行迭代。问题是这只返回顶级问题。我做错了什么?

整个表单:

代码语言:javascript
运行
复制
class Form implements RecursiveIterator{
    private $id;
    private $caption;
    private $other_text;
    private $questions = array();
    private $current;

    private function __construct(DibiRow $row){
        $this->id = $row->id;
        $this->caption = $row->caption;
        $this->other_text = $row->other_text;
        $this->loadQuestions();
    }

    private function loadQuestions(){
        $questions = dibi::query('SELECT * FROM cyp_questions WHERE form_id = %i AND parent_id IS NULL', $this->id);
        while($question = $questions->fetch()) $this->questions[] = new Question($question->question_id, $question->type, $question->caption, $question->other_text, $question->triggers_unique == 1);
    }

    /**
     * @throws InvalidArgumentException
     * @param $id
     * @return Form
     */
    public static function loadById($id){
        $form = dibi::query('SELECT * FROM cyp_forms WHERE id = %i', $id)->fetch();
        if($form === false) throw new InvalidArgumentException('Form with id '.$id.' was not found.');

        return new Form($form);
    }

    /**
     * @throws FormFieldException
     * @return bool
     */
    public function validate($postfields){

    }

    public function getQuestions(){
        return $this->questions;
    }

    public function getChildren(){
        return $this->questions[$this->current];
    }

    public function hasChildren(){
        return count($this->questions) > 0;
    }

    public function current(){
        return $this->questions[$this->current];
    }

    public function key(){
        return $this->current;
    }

    public function next(){
        $this->current++;
    }

    public function rewind(){
        $this->current = 0;
    }

    public function valid(){
        return isset($this->questions[$this->current]);
    }
}

问题:

代码语言:javascript
运行
复制
class Question implements RecursiveIterator{
    private $id;
    private $type;
    private $answers = array();
    private $subquestions = array();
    private $other_text;
    private $triggers_unique;
    private $caption;
    private $current = 0;


    public function __construct($id, $type, $caption, $other_text = null, $triggers_unique = false){
        $this->id = $id;
        $this->type = $type;
        $this->caption = $caption;
        $this->other_text = $other_text;
        $this->triggers_unique = $triggers_unique;  
        $this->setSubQuestions();   

    }

    private function setSubQuestions(){
        $questions = dibi::query('SELECT * FROM cyp_questions WHERE parent_id = %i', $this->id);
        while($question = $questions->fetch()) $this->subquestions[] = new Question($question->question_id, $question->type, $question->caption, $question->other_text, $question->triggers_unique == 1);
    }

    public function getOtherText(){
        return $this->other_text;
    }

    public function getCaption(){
        return $this->caption;
    }

    public function addAnswer($answer){
        $this->answers[] = $answer;
    }

    public function getChildren(){
        return $this->subquestions[$this->current];
    }

    public function hasChildren(){
        return count($this->subquestions) > 0;
    }

    public function current(){
        return $this->subquestions[$this->current];
    }

    public function key(){
        return $this->id;
    }

    public function next(){
        ++$this->current;
    }

    public function rewind(){
        $this->current = 0; 
    }

    public function valid(){
        return isset($this->subquestions[$this->current]);
    }

    public function getAnswers(){
        return $this->answers;
    }


}

迭代:

代码语言:javascript
运行
复制
$form = Form::loadById(1);

foreach($form as $question){
    echo $question->getCaption().'<br />';  
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-11-12 21:24:51

要遍历RecursiveIterator,必须将其包装到RecursiveIteratorIterator中。

查看下面的示例

默认的迭代模式是仅列出树叶。如果还希望包含节点出现在迭代中,请将RecursiveIteratorIterator::SELF_FIRST作为第二个参数传递给RecursiveIteratorIterator的构造函数

票数 9
EN

Stack Overflow用户

发布于 2010-11-12 20:56:38

如你所见,here

代码语言:javascript
运行
复制
public RecursiveIterator RecursiveIterator::getChildren ( void )

Returns an iterator for the current iterator entry. 

该方法应该返回一个实现迭代器的对象。您的方法返回一个简单的数组。

我的猜测是返回类似如下的内容:

代码语言:javascript
运行
复制
public function getChildren(){
    return new Question($this->subquestions);
}

这是因为您使用的是递归迭代器,因此它应该具有相同类型的树的每个节点(迭代器)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4164623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档