You are here

public static function ArrayUtils::iteratorToArray in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/zendframework/zend-stdlib/src/ArrayUtils.php \Zend\Stdlib\ArrayUtils::iteratorToArray()

Convert an iterator to an array.

Converts an iterator to an array. The $recursive flag, on by default, hints whether or not you want to do so recursively.

Parameters

array|Traversable $iterator The array or Traversable object to convert:

bool $recursive Recursively check all nested structures:

Return value

array

Throws

Exception\InvalidArgumentException if $iterator is not an array or a Traversable object

5 calls to ArrayUtils::iteratorToArray()
AbstractCallback::setOptions in vendor/zendframework/zend-feed/src/PubSubHubbub/AbstractCallback.php
Process any injected configuration options
ClassMethods::setOptions in vendor/zendframework/zend-hydrator/src/ClassMethods.php
Publisher::setOptions in vendor/zendframework/zend-feed/src/PubSubHubbub/Publisher.php
Process any injected configuration options
StrategyChain::__construct in vendor/zendframework/zend-hydrator/src/Strategy/StrategyChain.php
Constructor
Subscriber::setOptions in vendor/zendframework/zend-feed/src/PubSubHubbub/Subscriber.php
Process any injected configuration options

File

vendor/zendframework/zend-stdlib/src/ArrayUtils.php, line 216

Class

ArrayUtils
Utility class for testing and manipulation of PHP arrays.

Namespace

Zend\Stdlib

Code

public static function iteratorToArray($iterator, $recursive = true) {
  if (!is_array($iterator) && !$iterator instanceof Traversable) {
    throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable object');
  }
  if (!$recursive) {
    if (is_array($iterator)) {
      return $iterator;
    }
    return iterator_to_array($iterator);
  }
  if (method_exists($iterator, 'toArray')) {
    return $iterator
      ->toArray();
  }
  $array = [];
  foreach ($iterator as $key => $value) {
    if (is_scalar($value)) {
      $array[$key] = $value;
      continue;
    }
    if ($value instanceof Traversable) {
      $array[$key] = static::iteratorToArray($value, $recursive);
      continue;
    }
    if (is_array($value)) {
      $array[$key] = static::iteratorToArray($value, $recursive);
      continue;
    }
    $array[$key] = $value;
  }
  return $array;
}