<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Create a RecursiveFilterIterator from a RecursiveIterator</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="class.recursivefilteriterator.html">RecursiveFilterIterator</a></div> <div class="next" style="text-align: right; float: right;"><a href="recursivefilteriterator.getchildren.html">RecursiveFilterIterator::getChildren</a></div> <div class="up"><a href="class.recursivefilteriterator.html">RecursiveFilterIterator</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="recursivefilteriterator.construct" class="refentry"> <div class="refnamediv"> <h1 class="refname">RecursiveFilterIterator::__construct</h1> <p class="verinfo">(PHP 5 >= 5.1.0)</p><p class="refpurpose"><span class="refname">RecursiveFilterIterator::__construct</span> — <span class="dc-title">Create a RecursiveFilterIterator from a RecursiveIterator</span></p> </div> <div class="refsect1 description" id="refsect1-recursivefilteriterator.construct-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="methodname"><strong>RecursiveFilterIterator::__construct</strong></span> ( <span class="methodparam"><span class="type"><a href="class.recursiveiterator.html" class="type RecursiveIterator">RecursiveIterator</a></span> <code class="parameter">$iterator</code></span> )</div> <p class="para rdfs-comment"> Create a <a href="class.recursivefilteriterator.html" class="classname">RecursiveFilterIterator</a> from a <a href="class.recursiveiterator.html" class="classname">RecursiveIterator</a>. </p> </div> <div class="refsect1 parameters" id="refsect1-recursivefilteriterator.construct-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">iterator</code></em></span> <dd> <p class="para"> The <a href="class.recursiveiterator.html" class="classname">RecursiveIterator</a> to be filtered. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-recursivefilteriterator.construct-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> No value is returned. </p> </div> <div class="refsect1 examples" id="refsect1-recursivefilteriterator.construct-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-3895"> <p><strong>Example #1 Basic <span class="methodname"><strong>RecursiveFilterIterator()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">TestsOnlyFilter </span><span style="color: #007700">extends </span><span style="color: #0000BB">RecursiveFilterIterator </span><span style="color: #007700">{<br /> public function </span><span style="color: #0000BB">accept</span><span style="color: #007700">() {<br /> </span><span style="color: #FF8000">// Accept the current item if we can recurse into it<br /> // or it is a value starting with "test"<br /> </span><span style="color: #007700">return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">hasChildren</span><span style="color: #007700">() || (</span><span style="color: #0000BB">strpos</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">current</span><span style="color: #007700">(), </span><span style="color: #DD0000">"test"</span><span style="color: #007700">) !== </span><span style="color: #0000BB">FALSE</span><span style="color: #007700">);<br /> }<br />}<br /><br /></span><span style="color: #0000BB">$array </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"test1"</span><span style="color: #007700">, array(</span><span style="color: #DD0000">"taste2"</span><span style="color: #007700">, </span><span style="color: #DD0000">"test3"</span><span style="color: #007700">, </span><span style="color: #DD0000">"test4"</span><span style="color: #007700">), </span><span style="color: #DD0000">"test5"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$iterator </span><span style="color: #007700">= new </span><span style="color: #0000BB">RecursiveArrayIterator</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$filter </span><span style="color: #007700">= new </span><span style="color: #0000BB">TestsOnlyFilter</span><span style="color: #007700">(</span><span style="color: #0000BB">$iterator</span><span style="color: #007700">);<br /><br />foreach(new </span><span style="color: #0000BB">RecursiveIteratorIterator</span><span style="color: #007700">(</span><span style="color: #0000BB">$filter</span><span style="color: #007700">) as </span><span style="color: #0000BB">$key </span><span style="color: #007700">=> </span><span style="color: #0000BB">$value</span><span style="color: #007700">)<br />{<br /> echo </span><span style="color: #0000BB">$value </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> test1 test3 test4 test5 </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-3896"> <p><strong>Example #2 <span class="methodname"><strong>RecursiveFilterIterator()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">StartsWithFilter </span><span style="color: #007700">extends </span><span style="color: #0000BB">RecursiveFilterIterator </span><span style="color: #007700">{<br /><br /> protected </span><span style="color: #0000BB">$word</span><span style="color: #007700">;<br /><br /> public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">RecursiveIterator $rit</span><span style="color: #007700">, </span><span style="color: #0000BB">$word</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">word </span><span style="color: #007700">= </span><span style="color: #0000BB">$word</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$rit</span><span style="color: #007700">);<br /> }<br /><br /> public function </span><span style="color: #0000BB">accept</span><span style="color: #007700">() {<br /> return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">hasChildren</span><span style="color: #007700">() OR </span><span style="color: #0000BB">strpos</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">current</span><span style="color: #007700">(), </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">word</span><span style="color: #007700">) === </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /> }<br /> <br /> public function </span><span style="color: #0000BB">getChildren</span><span style="color: #007700">() {<br /> return new </span><span style="color: #0000BB">self</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">getInnerIterator</span><span style="color: #007700">()-></span><span style="color: #0000BB">getChildren</span><span style="color: #007700">(), </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">word</span><span style="color: #007700">);<br /> }<br />}<br /><br /></span><span style="color: #0000BB">$array </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"test1"</span><span style="color: #007700">, array(</span><span style="color: #DD0000">"taste2"</span><span style="color: #007700">, </span><span style="color: #DD0000">"test3"</span><span style="color: #007700">, </span><span style="color: #DD0000">"test4"</span><span style="color: #007700">), </span><span style="color: #DD0000">"test5"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$iterator </span><span style="color: #007700">= new </span><span style="color: #0000BB">RecursiveArrayIterator</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$filter </span><span style="color: #007700">= new </span><span style="color: #0000BB">StartsWithFilter</span><span style="color: #007700">(</span><span style="color: #0000BB">$iterator</span><span style="color: #007700">, </span><span style="color: #DD0000">"test"</span><span style="color: #007700">);<br /><br />foreach(new </span><span style="color: #0000BB">RecursiveIteratorIterator</span><span style="color: #007700">(</span><span style="color: #0000BB">$filter</span><span style="color: #007700">) as </span><span style="color: #0000BB">$key </span><span style="color: #007700">=> </span><span style="color: #0000BB">$value</span><span style="color: #007700">)<br />{<br /> echo </span><span style="color: #0000BB">$value </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> test1 test3 test4 test5 </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-recursivefilteriterator.construct-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="methodname"><a href="recursivefilteriterator.getchildren.html" class="methodname" rel="rdfs-seeAlso">RecursiveFilterIterator::getChildren()</a> - Return the inner iterator's children contained in a RecursiveFilterIterator</span></li> <li class="member"> <span class="methodname"><a href="recursivefilteriterator.haschildren.html" class="methodname" rel="rdfs-seeAlso">RecursiveFilterIterator::hasChildren()</a> - Check whether the inner iterator's current element has children</span></li> <li class="member"> <span class="methodname"><a href="filteriterator.accept.html" class="methodname" rel="rdfs-seeAlso">FilterIterator::accept()</a> - Check whether the current element of the iterator is acceptable</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="class.recursivefilteriterator.html">RecursiveFilterIterator</a></div> <div class="next" style="text-align: right; float: right;"><a href="recursivefilteriterator.getchildren.html">RecursiveFilterIterator::getChildren</a></div> <div class="up"><a href="class.recursivefilteriterator.html">RecursiveFilterIterator</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>