PHP RecursiveIterator遍历

前端之家收集整理的这篇文章主要介绍了PHP RecursiveIterator遍历前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个表示表单的结构,我想使用RecursiveIterator迭代它.
问题是这只会返回顶级问题.我究竟做错了什么?

整体形式:

  1. class Form implements RecursiveIterator{
  2. private $id;
  3. private $caption;
  4. private $other_text;
  5. private $questions = array();
  6. private $current;
  7.  
  8. private function __construct(DibiRow $row){
  9. $this->id = $row->id;
  10. $this->caption = $row->caption;
  11. $this->other_text = $row->other_text;
  12. $this->loadQuestions();
  13. }
  14.  
  15. private function loadQuestions(){
  16. $questions = dibi::query('SELECT * FROM cyp_questions WHERE form_id = %i AND parent_id IS NULL',$this->id);
  17. while($question = $questions->fetch()) $this->questions[] = new Question($question->question_id,$question->type,$question->caption,$question->other_text,$question->triggers_unique == 1);
  18. }
  19.  
  20. /**
  21. * @throws InvalidArgumentException
  22. * @param $id
  23. * @return Form
  24. */
  25. public static function loadById($id){
  26. $form = dibi::query('SELECT * FROM cyp_forms WHERE id = %i',$id)->fetch();
  27. if($form === false) throw new InvalidArgumentException('Form with id '.$id.' was not found.');
  28.  
  29. return new Form($form);
  30. }
  31.  
  32. /**
  33. * @throws FormFieldException
  34. * @return bool
  35. */
  36. public function validate($postfields){
  37.  
  38. }
  39.  
  40. public function getQuestions(){
  41. return $this->questions;
  42. }
  43.  
  44. public function getChildren(){
  45. return $this->questions[$this->current];
  46. }
  47.  
  48. public function hasChildren(){
  49. return count($this->questions) > 0;
  50. }
  51.  
  52. public function current(){
  53. return $this->questions[$this->current];
  54. }
  55.  
  56. public function key(){
  57. return $this->current;
  58. }
  59.  
  60. public function next(){
  61. $this->current++;
  62. }
  63.  
  64. public function rewind(){
  65. $this->current = 0;
  66. }
  67.  
  68. public function valid(){
  69. return isset($this->questions[$this->current]);
  70. }
  71. }

题:

  1. class Question implements RecursiveIterator{
  2. private $id;
  3. private $type;
  4. private $answers = array();
  5. private $subquestions = array();
  6. private $other_text;
  7. private $triggers_unique;
  8. private $caption;
  9. private $current = 0;
  10.  
  11.  
  12. public function __construct($id,$type,$caption,$other_text = null,$triggers_unique = false){
  13. $this->id = $id;
  14. $this->type = $type;
  15. $this->caption = $caption;
  16. $this->other_text = $other_text;
  17. $this->triggers_unique = $triggers_unique;
  18. $this->setSubQuestions();
  19.  
  20. }
  21.  
  22. private function setSubQuestions(){
  23. $questions = dibi::query('SELECT * FROM cyp_questions WHERE parent_id = %i',$this->id);
  24. while($question = $questions->fetch()) $this->subquestions[] = new Question($question->question_id,$question->triggers_unique == 1);
  25. }
  26.  
  27. public function getOtherText(){
  28. return $this->other_text;
  29. }
  30.  
  31. public function getCaption(){
  32. return $this->caption;
  33. }
  34.  
  35. public function addAnswer($answer){
  36. $this->answers[] = $answer;
  37. }
  38.  
  39. public function getChildren(){
  40. return $this->subquestions[$this->current];
  41. }
  42.  
  43. public function hasChildren(){
  44. return count($this->subquestions) > 0;
  45. }
  46.  
  47. public function current(){
  48. return $this->subquestions[$this->current];
  49. }
  50.  
  51. public function key(){
  52. return $this->id;
  53. }
  54.  
  55. public function next(){
  56. ++$this->current;
  57. }
  58.  
  59. public function rewind(){
  60. $this->current = 0;
  61. }
  62.  
  63. public function valid(){
  64. return isset($this->subquestions[$this->current]);
  65. }
  66.  
  67. public function getAnswers(){
  68. return $this->answers;
  69. }
  70.  
  71.  
  72. }

迭代:

  1. $form = Form::loadById(1);
  2.  
  3. foreach($form as $question){
  4. echo $question->getCaption().'<br />';
  5. }
要遍历RecursiveIterator,必须将其包装到RecursiveIteratorIterator中.

看一些例子

> Introduction to Spl
> SplWiki

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

猜你在找的PHP相关文章