{"id":468,"date":"2010-01-07T10:58:54","date_gmt":"2010-01-07T10:58:54","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=468"},"modified":"2010-01-07T10:59:35","modified_gmt":"2010-01-07T10:59:35","slug":"wordsearch-word-class-iterator","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/07\/wordsearch-word-class-iterator\/","title":{"rendered":"Wordsearch &#8211; Word Class iterator"},"content":{"rendered":"<p>An iterator is when a object can be used within a foreach loop and each data item can be looped through, with a couple of other functions declared as well e.g. key, next because these functions allow the foreach loop to work.  For example, at the start of the object you will need to have a position indicator (set normally at 0) and a object (array) to go through, I am using the <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/01\/05\/wordsearch-php-words-to-search-for\/\">word<\/a> class from the word search project.  From the php.net website there is a more details of a <a href=\"http:\/\/uk3.php.net\/iterator\">iterator<\/a>.<\/p>\n<p>The foreach loop basically starts the iterator from the starting point (rewind function) and then using the next function call to increment the internal object to its next point whilst making sure that it has not come to its end point (checking against the valid function).<\/p>\n<p>Here is the code that I have used to demonstrate a iterator interface (<a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/01\/06\/interfaces-the-abstract-class\/\">interface<\/a> example).<\/p>\n<p>Please note, extending from the <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/01\/05\/wordsearch-php-words-to-search-for\/\">word<\/a> class from a previous post.<\/p>\n<pre lang=\"php\">\r\nclass WordIterator extends Word implements Iterator {\r\n    private $_position = 0;\r\n    private $_wordsIterator;\r\n    \r\n    public function __construct($wordsOrFilename = \"words.xml\", $maxSearchNum = 5) {\r\n        $this->_position = 0;\r\n\t$this->_wordsIterator = array();\r\n\r\n\tparent::__construct($wordsOrFilename, $maxSearchNum);\r\n\t\r\n\t$this->_wordsIterator = parent::wordsArray();\r\n    }\r\n\r\n    function rewind() {\r\n        $this->_position = 0;\r\n    }\r\n    \r\n    function current() {\r\n        return $this->_wordsIterator[$this->_position];\r\n    }\r\n\r\n    function key() {\r\n        return $this->_position;\r\n    }\r\n\r\n    function next() {\r\n        ++$this->_position;\r\n    }\r\n\r\n    function valid() {\r\n        return isset($this->_wordsIterator[$this->_position]);\r\n    }\r\n}\r\n\r\n\r\n$WordIt = new WordIterator;\r\n\r\nforeach($WordIt as $key => $value) {\r\n    echo $key . \" \" . $value;\r\n    echo \"\\n\";\r\n}\r\n<\/pre>\n<p>Output would be <\/p>\n<pre lang=\"bash\">\r\n0 merry\r\n1 old \r\n2 sole \r\n3 was \r\n4 he \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>An iterator is when a object can be used within a foreach loop and each data item can be looped through, with a couple of other functions declared as well e.g. key, next because these functions allow the foreach loop to work. For example, at the start of the object you will need to have &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/07\/wordsearch-word-class-iterator\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Wordsearch &#8211; Word Class iterator<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[51],"class_list":["post-468","post","type-post","status-publish","format-standard","hentry","category-php","tag-iterator"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=468"}],"version-history":[{"count":3,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/468\/revisions"}],"predecessor-version":[{"id":471,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/468\/revisions\/471"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}