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(); } }
/** * Searches value inside a multidimensional array, returning its index * * Original function by "giulio provasi" (link below) * * @param mixed|array $haystack * The haystack to search * * @param mixed $needle * The needle we are looking for * * @param mixed|optional $index * Allow to define a specific index where the data will be searched * * @return integer|string * If given needle can be found in given haystack, its index will * be returned. Otherwise, -1 will * * @see http://www.php.net/manual/en/function.array-search.php#97645 */ public static function search($haystack, $needle, $index = NULL) { if (is_null($haystack)) { return -1; } $arrayIterator = new \RecursiveArrayIterator($haystack); $iterator = new \RecursiveIteratorIterator($arrayIterator); while ($iterator->valid()) { if ((isset($index) and $iterator->key() == $index or !isset($index)) and $iterator->current() == $needle) { return $arrayIterator->key(); } $iterator->next(); } return -1; }
/** * Find a named route in a given array of routes. * * @param string $name * @param array $routes * @return array */ public static function find($name, $routes) { if (array_key_exists($name, static::$names)) { return static::$names[$name]; } $arrayIterator = new \RecursiveArrayIterator($routes); $recursiveIterator = new \RecursiveIteratorIterator($arrayIterator); // Since routes can be nested deep within sub-directories, we need to recursively // iterate through each directory and gather all of the routes. foreach ($recursiveIterator as $iterator) { $route = $recursiveIterator->getSubIterator(); if (isset($route['name']) and $route['name'] == $name) { return static::$names[$name] = array($arrayIterator->key() => iterator_to_array($route)); } } }
/** @return key() since that is the name we need */ function current() { $result = parent::key(); $parent = get_parent_class($result); if ($parent) { $interfaces = array_diff(class_implements($result), class_implements($parent)); if ($interfaces) { $implements = array(); foreach ($interfaces as $interface) { $implements = array_merge($implements, class_implements($interface)); } $interfaces = array_diff($interfaces, $implements); natcasesort($interfaces); $result .= ' (' . join(', ', $interfaces) . ')'; } } return $result; }
private function search_in_array($needle, $haystack) { $array_iterator = new RecursiveArrayIterator($haystack); $iterator = new RecursiveIteratorIterator($array_iterator); while ($iterator->valid()) { if ($iterator->current() == $needle) { return $array_iterator->key(); } $iterator->next(); } return false; }
/** * Unset empty * @param $values * @return array */ protected function unsetEmpty($values) { if (empty($values)) { return $values; } $values = array_filter($values, function ($value) { return $value !== null && $value !== ''; }); $result = array(); $iterator = new RecursiveArrayIterator($values); $recursive = new RecursiveIteratorIterator($iterator); foreach ($recursive as $key => $value) { $value = JString::trim($value); if ($value !== '' && $value !== null) { $depth = $recursive->getDepth(); $depth--; $subKey = null; $subIterator = $recursive->getSubIterator($depth); if ($subIterator) { $subKey = $subIterator->key(); } if ($subKey && $subKey != $iterator->key()) { $result[$iterator->key()][$subKey][$key] = $value; } elseif ($iterator->key() != $key) { $result[$iterator->key()][$key] = $value; } else { $result[$key] = $value; } } } return $result; }
public function getColumns($table = NULL) { if ($table === NULL) { throw new \Exception("Please input a table name. - glDbMysql.php - line 89"); } if (!in_array($table, $this->getTables())) { throw new \Exception("The table you're trying to query does not exist - glDbMysql.php - line 91"); } $dbh = $this->getDbh(); $stmt = $dbh->prepare("DESCRIBE " . $table); $success = $stmt->execute(); $columnNames = $stmt->fetchAll(\PDO::FETCH_ASSOC); $iterator = new \RecursiveArrayIterator($columnNames); $fields = array(); if ($success !== FALSE) { while ($iterator->valid()) { if ($iterator->hasChildren()) { $childIterator = new \RecursiveArrayIterator($iterator->current()); while ($childIterator->valid()) { if ($childIterator->key() == "Field") { array_push($fields, $childIterator->current()); } $childIterator->next(); } } $iterator->next(); } return $fields; } $error = array('query' => "DESCRIBE " . $table, 'errorInfo' => $this->prepareErrorInfo($dbh->errorInfo())); $errorInfo = json_encode($error); throw new MysqlException($errorInfo); }
/** * 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(); } }
/** * 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(); } }
/** @return key() since that is the name we need */ function current() { return parent::key(); }
<?php /*** an array of animal ***/ $animals = array(array('type' => 'dog', 'name' => 'butch', 'sex' => 'm', 'breed' => 'boxer'), array('type' => 'dog', 'name' => 'fido', 'sex' => 'm', 'breed' => 'doberman'), array('type' => 'dog', 'name' => 'girly', 'sex' => 'f', 'breed' => 'poodle'), array('type' => 'cat', 'name' => 'tiddles', 'sex' => 'm', 'breed' => 'ragdoll'), array('type' => 'cat', 'name' => 'tiddles', 'sex' => 'f', 'breed' => 'manx'), array('type' => 'cat', 'name' => 'tiddles', 'sex' => 'm', 'breed' => 'maine coon'), array('type' => 'horse', 'name' => 'ed', 'sex' => 'm', 'breed' => 'clydesdale'), array('type' => 'perl_coder', 'name' => 'shadda', 'sex' => 'none', 'breed' => 'mongrel'), array('type' => 'duck', 'name' => 'galapogus', 'sex' => 'm', 'breed' => 'pekin')); /*** create a new recursive array iterator ***/ $iterator = new RecursiveArrayIterator(new ArrayObject($animals)); /*** traverse the $iterator object ***/ while ($iterator->valid()) { echo $iterator->key() . ' -- ' . $iterator->current() . '<br/>'; $iterator->next(); }
/** * @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(); } }
/** * performs a search in a nested array * @param array $haystack the array to be searched * @param string $needle the search string * @param string $index optional, only search this key name * @return mixed the key of the matching field, otherwise false * * performs a search in a nested array * * taken from http://www.php.net/manual/en/function.array-search.php#97645 */ public static function recursiveArraySearch($haystack, $needle, $index = null) { $aIt = new RecursiveArrayIterator($haystack); $it = new RecursiveIteratorIterator($aIt); while ($it->valid()) { if ((isset($index) and $it->key() == $index or !isset($index)) and $it->current() == $needle) { return $aIt->key(); } $it->next(); } return false; }
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; }
/** * Find a value also in nested arrays/objects * * @param mixed $needle The value to search for * * @return string The key of that value * * @since 1.0.0 */ public function searchRecursive($needle) { $aIt = new RecursiveArrayIterator($this); $it = new RecursiveIteratorIterator($aIt); while ($it->valid()) { if ($it->current() == $needle) { return $aIt->key(); } $it->next(); } return false; }
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) . '}'; } }
/** * * Search for needle in a recursive array * @author http://www.php.net/manual/en/function.array-search.php#97645 * * @param $haystack * @param $needle * @param $index */ function rarray_search($needle, $haystack, $index = null) { $aIt = new RecursiveArrayIterator($haystack); $it = new RecursiveIteratorIterator($aIt); // Tar bort ".www" om det finns för bättre jämföring $needle = preg_replace('/\bwww./', '', $needle); while($it->valid()) { // Tar bort ".www" om det finns för bättre jämföring $current = preg_replace('/\bwww./', '', $it->current()); if (((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND ($current == $needle)) { return $aIt->key(); } $it->next(); } return FALSE; }