function traverse(RecursiveArrayIterator $iterator) { while ($iterator->valid()) { if ($iterator->hasChildren()) { printf("HasChild: %s\n", $iterator->key()); traverse($iterator->getChildren()); } else { printf("%s => %s\n", $iterator->key(), $iterator->current()); } $iterator->next(); } }
/** * Walks through array * @param $array * @param $callback callable function($path, $value) */ public static function walkArray($array, $callback, $iterator = null, $prefix = '') { if (is_null($iterator)) { $iterator = new \RecursiveArrayIterator($array); } while ($iterator->valid()) { if ($iterator->hasChildren()) { self::walkArray(null, $callback, $iterator->getChildren(), $prefix . '.' . $iterator->key()); } else { call_user_func($callback, ltrim($prefix . '.' . $iterator->key(), '.'), $iterator->current()); } $iterator->next(); } }
private function parseJson(\RecursiveArrayIterator $json, array $data, $level) { foreach ($json as $k => $j) { if (!isset($this->path[$level])) { return $data; } if ($k !== $this->path[$level] && $this->path[$level] !== '*') { continue; } if ($k === end($this->path)) { if (is_array($j)) { $data = array_merge($data, $j); } else { $data[] = $j; } } if ($json->hasChildren()) { $data = $this->parseJson($json->getChildren(), $data, $level + 1); } } return $data; }
function getChildren() { echo __METHOD__ . "\n"; return parent::getChildren(); }
/** * @param RecursiveArrayIterator $iterator */ public function fetchCategoriesWithProducts(RecursiveArrayIterator $iterator) { while ($iterator->valid()) { if ($iterator->hasChildren()) { $this->fetchCategoriesWithProducts($iterator->getChildren()); } else { if ($iterator->key() == 'countProducts' && $iterator->current() != '0') { $this->_categoryWithProducts[$iterator->offsetGet('id')] = array('name' => $iterator->offsetGet('name'), 'full_path' => $iterator->offsetGet('full_path'), 'countProduct' => $iterator->offsetGet('countProducts')); } /*$this->_categoryWithProducts[$iterator->offsetGet('id')] = $iterator->offsetGet('countProducts');*/ } $iterator->next(); } }
/** * Find and return the bottom node in the given tree * @param \RecursiveArrayIterator $iterator * @param array $tree * @return mixed Array with 0: iterator, 1: value (reference) */ protected function findNode(\RecursiveArrayIterator $iterator, $tree = []) { $find_key = array_shift($tree); foreach ($iterator as $key => &$value) { if ($key !== $find_key) { continue; } # $tree isn't null yet, meaning we still have to travel down # nodes in order to get to the last one... inception if (isset($tree[0])) { return $this->findNode($iterator->getChildren(), $tree); } # Return a reference to this current node - it's needed later. More details # are in the findValue() function. Yeah, it's kinda hackey return [$iterator, &$value]; } return null; }
public function getChildren() { echo "MyArrIter::getChildren\n"; return parent::getChildren(); }
function getChildren() { echo __METHOD__ . "()\n"; self::fail(2, __METHOD__); return parent::getChildren(); }
function traverse_structure($ids) { $return_ids = array(); $iterator = new RecursiveArrayIterator($ids); while ($iterator->valid()) { if ($iterator->hasChildren()) { $return_ids = array_merge($return_ids, $this->traverse_structure($iterator->getChildren())); } else { if ($iterator->key() == 'int') { $return_ids = array_merge($return_ids, array($iterator->current())); } } $iterator->next(); } return $return_ids; }
/** * Walks through input array * * @param $array * @param $callback callable function($path, $value) * @param null $iterator * @param string $prefix */ private function walkInputArray($array, $callback, $iterator = null, $prefix = '') { if (!$iterator) { $iterator = new \RecursiveArrayIterator($array); } while ($iterator->valid()) { $key = $iterator->key(); if ($iterator->hasChildren()) { $this->walkInputArray(null, $callback, $iterator->getChildren(), $prefix . '.' . $key); } else { call_user_func($callback, ltrim($prefix . '.' . $key, '.'), $iterator->current()); } $iterator->next(); } }
private function highlightElement($indexArray, $menuArray = null, $nr = 0) { //index to get $i = $indexArray[$nr]; //if menu is null take menuArray if ($menuArray == null) { $menuArray = $this->menuArray; } //create itterator $iter = new RecursiveArrayIterator($menuArray); //move to current index $iter->seek($i); //move down until we have found the element - then set the css-class for that element if (is_array($iter->current())) { $this->highlightElement($indexArray, $iter->getChildren(), $nr + 1); } else { $iter->current()->cssClass = $this->cssClass; } }
/** * Helper to `names`, which recursively traverses the iterator appending * new keys onto the base-so-far. * * @param \RecursiveArrayIterator $it * @param string $base * @param array $names */ private function reducer(\RecursiveArrayIterator $it, $base, &$names) { while ($it->valid()) { $sub_base = sprintf('%s[%s]', $base, $it->key()); if ($it->hasChildren()) { $this->reducer($it->getChildren(), $sub_base); } else { $names[] = $sub_base; } $it->next(); } }
<?php class Persona { public $nome = 'Mario'; public $cognome = 'Rossi'; public $telefono = array('cellulare' => '114499', 'ufficio' => '224433'); public $socials = array('twitter' => 'rossi', 'facebook' => 'http://www.facebook.com/rossi'); } $iterator = new RecursiveArrayIterator(new Persona()); foreach ($iterator as $index => $value) { if ($iterator->hasChildren()) { foreach ($iterator->getChildren() as $key => $child) { echo $key . ': ' . $child, PHP_EOL; } } else { echo $index . ': ' . $value, PHP_EOL; } }
protected static function encode_iterator(RecursiveArrayIterator $iterator, $options, $depth) { $json = array(); while ($iterator->valid()) { $key = $iterator->key(); $value = $iterator->current(); if ($value instanceof JSExpression) { var_dump($value); $json[$key] = $value->json_encode($options); } else { if ($iterator->hasChildren()) { if (!($depth > 0)) { throw new Exception("Maximum depth reached"); } $json[$key] = static::encode_iterator($iterator->getChildren(), $options, $depth - 1); } else { $json[$key] = static::encode_value($iterator->current(), $options); } } $iterator->next(); } var_dump($json); if (self::is_numeric_array($json)) { return '[' . implode(",", $json) . ']'; } else { foreach (array_keys($json) as $key) { $json[$key] = static::encode_key($key, $options) . ':' . $json[$key]; } return '{' . implode(",", $json) . '}'; } }