flatten() public method

Get a flattened array of the items in the collection.
public flatten ( integer $depth = INF ) : static
$depth integer
return static
Example #1
0
 public function testFlattenWithDepth()
 {
     // No depth flattens recursively
     $c = new Collection([['#foo', ['#bar', ['#baz']]], '#zap']);
     $this->assertEquals(['#foo', '#bar', '#baz', '#zap'], $c->flatten()->all());
     // Specifying a depth only flattens to that depth
     $c = new Collection([['#foo', ['#bar', ['#baz']]], '#zap']);
     $this->assertEquals(['#foo', ['#bar', ['#baz']], '#zap'], $c->flatten(1)->all());
     $c = new Collection([['#foo', ['#bar', ['#baz']]], '#zap']);
     $this->assertEquals(['#foo', '#bar', ['#baz'], '#zap'], $c->flatten(2)->all());
 }
 /**
  * Get the middleware for the controller instance.
  *
  * @param  \Illuminate\Routing\Controller  $instance
  * @param  string  $method
  * @return array
  */
 protected function getMiddleware($instance, $method)
 {
     $results = new Collection();
     foreach ($instance->getMiddleware() as $name => $options) {
         if (!$this->methodExcludedByOptions($method, $options)) {
             $results[] = $this->router->resolveMiddlewareClassName($name);
         }
     }
     return $results->flatten()->all();
 }
 public function testFlattenIgnoresKeys()
 {
     // No depth ignores keys
     $c = new Collection(['#foo', ['key' => '#bar'], ['key' => '#baz'], 'key' => '#zap']);
     $this->assertEquals(['#foo', '#bar', '#baz', '#zap'], $c->flatten()->all());
     // Depth of 1 ignores keys
     $c = new Collection(['#foo', ['key' => '#bar'], ['key' => '#baz'], 'key' => '#zap']);
     $this->assertEquals(['#foo', '#bar', '#baz', '#zap'], $c->flatten(1)->all());
 }