Calls a function for each key-value pair in the set.
If recursive is true, will apply the callback to nested arrays as well.

@param array $set
@param \Closure $callback
@param bool $recursive
@return array
165425
public static function each(array $set, Closure $callback, $recursive = true) {
        foreach ($set as $key => $value) {
            if (is_array($value) && $recursive) {
                $set[$key] = static::each($value, $callback, $recursive);
            } else {
                $set[$key] = $callback($value, $key);
            }
        }

        return $set;
    }
1035391
public static function toArray($object, $recursive = true)
    {
        $array = [];

        if ($object instanceof BaseObject) {
            return $object->toArray($recursive);
        }

        foreach ($object as $key => $value) {
            if ($value instanceof BaseObject) {
                $newValue = $value->toArray($recursive);
            } else {
                $newValue = (is_array($value) || is_object($value)) && $recursive ? self::toArray($value) : $value;
            }

            $array[$key] = $newValue;
        }

        return $array;
    }
51733
public function getValue($recursive = false)
    {
        $item = $this->data[$this->position][$this->key];

        if ($item instanceof Stub && Stub::TYPE_REF === $item->type && !$item->position) {
            $item = $item->value;
        }
        if (!($item = $this->getStub($item)) instanceof Stub) {
            return $item;
        }
        if (Stub::TYPE_STRING === $item->type) {
            return $item->value;
        }

        $children = $item->position ? $this->data[$item->position] : [];

        foreach ($children as $k => $v) {
            if ($recursive && !($v = $this->getStub($v)) instanceof Stub) {
                continue;
            }
            $children[$k] = clone $this;
            $children[$k]->key = $k;
            $children[$k]->position = $item->position;

            if ($recursive) {
                if (Stub::TYPE_REF === $v->type && ($v = $this->getStub($v->value)) instanceof Stub) {
                    $recursive = (array) $recursive;
                    if (isset($recursive[$v->position])) {
                        continue;
                    }
                    $recursive[$v->position] = true;
                }
                $children[$k] = $children[$k]->getValue($recursive);
            }
        }

        return $children;
    }
84974
protected function bindOriginalRowModel(Closure $callback, $key)
    {
        $rowModel = static::$originalGridModels[$key];

        return $callback->bindTo($rowModel);
    }
1354602
public static function getOrSet($key, \Closure $closure)
    {
        $key = Cache::key($key);
        if (!isset(self::$cache[$key])) {
            self::$cache[$key] = $closure();
        }
        return self::$cache[$key];
    }
404514
public function toArray($recursive = false) {
        $return = get_object_vars($this);
        if ($recursive) {
            foreach ($return as $ppt => &$val) {
                if ($ppt === 'integerkeys')
                    continue;
                if (is_object($val) && method_exists($val, 'toArray')) {
                    $val = $val->toArray(true);
                }
            }
        }
        unset($return['integerKeys']);
        return $return;
    }
699875
public static function each(array &$storage, \Closure $callback, array $prefix = array())
    {
        foreach($storage as $key => &$value)
        {
            if(is_array($value))
                static::each($value, $callback, array_merge($prefix, array($key)));
            else
                $callback(implode('.', array_merge($prefix, array($key))), $value, $storage[$key]);
        }
    }
138147
public function toArray($recursive = true)
    {
        $array = [];
        $data  = $this->data;

        /** @var self $value */
        foreach ($data as $key => $value) {
            if ($recursive && $value instanceof self) {
                $array[$key] = $value->toArray();
            } else {
                $array[$key] = $value;
            }
        }

        return $array;
    }
402549
public static function settype(array $array, $type, $recursive = true)
    {
        foreach ($array as $k => $v) {
            if ($recursive && is_array($v)) {
                $array[$k] = self::settype($v, $type, $recursive);
            } else {
                settype($array[$k], $type);
            }
        }
        
        return $array;
    }
479566
protected function _cache($key, Closure $callback) {
        if (isset($this->_cache[$key])) {
            return $this->_cache[$key];
        }

        // Requires 5.4
        // Closure::bind($callback, $this, __CLASS__);

        $this->_cache[$key] = $callback($this);

        return $this->_cache[$key];
    }
622748
public function recursive($recursive = true)
    {
        $this->recursive = true;

        if ($recursive === false) {
            $this->recursive = false;
        }

        return $this;
    }
1253531
public function callback(Closure $callback)
    {
        foreach ($this->getInnerIterator() as $key => $value) {
            yield $callback($value, $key);
        }
    }
591420
public function depthFirst($tree = [], \Closure $callback = null)
    {
        if (!$tree) {
            $tree = $this->tree;
        }
        foreach ($tree as $node) {
            if ($callback) {
                call_user_func_array($callback, ['node' => $node]);
            }
            $children = $node['children'];
            if ($children) {
                $this->depthFirst($children, $callback);
            }
        }
    }
440638
public function collateChildren(Closure $criteria, $recursive = true)
    {
        $entity_map = new EntityMap;
        $nested_attribute_types = [ EmbeddedEntityListAttribute::CLASS, EntityReferenceListAttribute::CLASS ];
        foreach ($this->getType()->getAttributes([], $nested_attribute_types) as $attribute) {
            foreach ($this->getValue($attribute->getName()) as $child_entity) {
                if ($criteria($child_entity)) {
                    $entity_map->setItem($child_entity->asEmbedPath(), $child_entity);
                }
                if ($recursive) {
                    $entity_map->append($child_entity->collateChildren($criteria));
                }
            }
        }
        return $entity_map;
    }
9280
private static function _toArray($data, $recursion = false)
    {
        $tmp  = array();
        $data = (array) $data;
        foreach ($data as $k => $v) {
            $v = (array) $v;
            if (isset($v[0]) && is_string($v[0])) {
                $tmp[$k] = $v[0];
            } else {
                $tmp[$k] = self::_toArray($v, true);
            }
        }
        return $tmp;
    }
796750
public function filter(\Closure $closure, $keepKeys = false)
    {
        $collection = new Collection();

        $i = 0;
        foreach ($this->items as $key => $value) {
            if ( ! $closure($value, $key, $i++)) {
                $keepKeys || $key = null;
                $collection->add($value, $key);
            }
        }

        return $collection;
    }
485588
public function map(Closure $callback)
    {
        return new self(array_map($callback, $this->items, array_keys($this->items)));
    }