Illuminate\Support\Collection::getIterator PHP Method

getIterator() public method

Get an iterator for the items.
    public function getIterator()
    {
        return new ArrayIterator($this->items);
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Get the instance as an array.
  *
  * @return array
  */
 public function toArray()
 {
     if ($this->channels instanceof Collection) {
         // 先拿到第一个子级频道项,并拿到它的父级频道ID
         // 开始遍历整个频道栏目,但是跳过第一条记录
         // 当然,每当遇到父级频道ID与当前频道ID相同
         // 则该为根级频道,一次搜索完成
         $first = $this->channels->first();
         $cursor = $first->channel_id;
         $iterator = $this->channels->getIterator();
         while ($iterator->valid() && ($channel = $iterator->current())) {
             echo 'prepare' . PHP_EOL;
             if ($channel->channel_id == $cursor || $channel->flag === 1) {
                 $iterator->next();
                 continue;
             }
             echo 'start' . PHP_EOL;
             var_dump($first->channel_id, $first->channel_parent);
             // 当一次搜索完成时,重新搜索数据集
             if (isset($_parent) && $_parent->channel_id === $_parent->channel_parent) {
                 echo 'rewind' . PHP_EOL;
                 $iterator->rewind();
                 continue;
             }
             if ($channel->channel_id === $first->channel_parent) {
                 $_parent = $channel;
                 echo 'merge' . PHP_EOL;
                 var_dump($_parent->channel_id);
                 $this->{$_parent->channel_type}[$_parent->channel_name] = array_merge($this->{$_parent->channel_type}, ['data' => $first, 'idx' => $cursor, 'to' => $_parent->channel_id]);
                 echo 'to wind' . PHP_EOL;
                 var_dump($this->{$_parent->channel_type});
                 // 因为已经设置过子父级关系,则当前数据项设置为1
                 $cursor = $_parent->channel_id;
                 $first = $_parent;
                 $channel->flag = 1;
             }
             echo 'complete' . PHP_EOL;
             $iterator->next();
         }
     }
     return array_merge(['default' => $this->default], ['mega' => $this->mega]);
 }
All Usage Examples Of Illuminate\Support\Collection::getIterator