Skip to content

Instantly share code, notes, and snippets.

@diimpp
Last active February 11, 2017 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diimpp/533a658aaf637c151643240e6e1acfc8 to your computer and use it in GitHub Desktop.
Save diimpp/533a658aaf637c151643240e6e1acfc8 to your computer and use it in GitHub Desktop.
Modifiable RecursiveIteratorIterator/RecursiveArrayIterator
<?php
$array = [
[
'name' => 'level1-1',
'children' => [],
],
[
'name' => 'level1-2',
'children' => [
[
'name' => 'level2-1',
'children' => [
[
'name' => 'level3-1',
'children' => [],
],
[
'name' => 'level3-2',
'children' => [
[
'name' => 'level4-1',
'children' => [],
],
],
],
],
],
],
],
[
'name' => 'level1-3',
'children' => [
[
'name' => 'level2-2',
'children' => [
[
'name' => 'level3-3',
'children' => [],
],
],
],
],
],
];
class MyRecursiveArrayIterator extends \ArrayIterator implements \RecursiveIterator
{
- public function hasChildren()
| {
| $current = $this->current();
|
| return is_array($current['children']) && count($current['children']);
| }
|
- public function getChildren()
| {
| $current = $this->current();
|
| return new self($current['children']);
| }
}
$arrayIterator = new MyRecursiveArrayIterator($array);
$iterator = new \RecursiveIteratorIterator($arrayIterator, \RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $key => $value) {
$currentDepth = $iterator->getDepth();
for ($subDepth = $currentDepth; $subDepth >= 0; $subDepth--) {
$subIterator = $iterator->getSubIterator($subDepth);
if ($currentDepth === $subDepth) {
$value['name'] = $value['name'].'MODIFIED';
$subIterator->offsetSet($subIterator->key(), $value);
} else {
$element = $subIterator->offsetGet($subIterator->key());
$upperSubIterator = $iterator->getSubIterator(($subDepth+1));
$element['children'] = $upperSubIterator->getArrayCopy();
$subIterator->offsetSet($subIterator->key(), $element);
}
}
}
var_dump($iterator->getArrayCopy());
/**
Output
array(3) {
[0]=>
array(2) {
["name"]=>
string(16) "level1-1MODIFIED"
["children"]=>
array(0) {
}
}
[1]=>
array(2) {
["name"]=>
string(16) "level1-2MODIFIED"
["children"]=>
array(1) {
[0]=>
array(2) {
["name"]=>
string(16) "level2-1MODIFIED"
["children"]=>
array(2) {
[0]=>
array(2) {
["name"]=>
string(16) "level3-1MODIFIED"
["children"]=>
array(0) {
}
}
[1]=>
array(2) {
["name"]=>
string(16) "level3-2MODIFIED"
["children"]=>
array(1) {
[0]=>
array(2) {
["name"]=>
string(16) "level4-1MODIFIED"
["children"]=>
array(0) {
}
}
}
}
}
}
}
}
[2]=>
array(2) {
["name"]=>
string(16) "level1-3MODIFIED"
["children"]=>
array(1) {
[0]=>
array(2) {
["name"]=>
string(16) "level2-2MODIFIED"
["children"]=>
array(1) {
[0]=>
array(2) {
["name"]=>
string(16) "level3-3MODIFIED"
["children"]=>
array(0) {
}
}
}
}
}
}
}
*/
@diimpp
Copy link
Author

diimpp commented Feb 11, 2017

Example of modifiable SPL RecursiveIteratorIterator with RecursiveArrayIterator
Solves:
http://stackoverflow.com/q/17867245
http://stackoverflow.com/q/1228575

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment