Created
February 16, 2016 14:28
-
-
Save useless-stuff/145437f586ff78777256 to your computer and use it in GitHub Desktop.
PHP - RecursiveCachingIterator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Class User | |
| */ | |
| class User | |
| { | |
| public $name; | |
| /** | |
| * User constructor. | |
| * @param $name | |
| */ | |
| public function __construct($name) | |
| { | |
| $this->name = $name; | |
| } | |
| /** | |
| * @return string | |
| */ | |
| public function __toString() | |
| { | |
| return (string)$this->name; | |
| } | |
| } | |
| $data = array( | |
| 'developers' => array( | |
| new User('David'), | |
| new User('Diego'), | |
| new User('John'), | |
| ), | |
| 'customer_care' => array( | |
| new User('Samantha'), | |
| new User('Luisa'), | |
| new User('Giovanni'), | |
| ), | |
| ); | |
| $iterator = new RecursiveCachingIterator(new RecursiveArrayIterator($data)); | |
| for ($iterator->rewind(); $iterator->valid(); $iterator->next()) { | |
| for ($iterator->getChildren()->rewind(); $iterator->getChildren()->valid(); $iterator->getChildren()->next()) { | |
| echo $iterator->getChildren().PHP_EOL; | |
| } | |
| } | |
| // Output: | |
| /* | |
| David | |
| Diego | |
| John | |
| Samantha | |
| Luisa | |
| Giovanni | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment