lithium\util\Collection::find PHP Method

find() public method

Filters a copy of the items in the collection.
public find ( callback $filter, array $options = [] ) : mixed
$filter callback Callback to use for filtering.
$options array The available options are: - `'collect'`: If `true`, the results will be returned wrapped in a new `Collection` object or subclass. Defaults to `true`.
return mixed The filtered items. Will be an array unless `'collect'` is defined in the `$options` argument, then an instance of this class will be returned.
    public function find($filter, array $options = array())
    {
        $defaults = array('collect' => true);
        $options += $defaults;
        $data = array_filter($this->_data, $filter);
        if ($options['collect']) {
            $class = get_class($this);
            $data = new $class(compact('data'));
        }
        return $data;
    }

Usage Example

Example #1
0
 /**
  * Tests that the `find()` method properly filters items out of the resulting collection.
  *
  * @return void
  */
 public function testCollectionFindFilter()
 {
     $collection = new Collection(array('data' => array_merge(array_fill(0, 10, 1), array_fill(0, 10, 2))));
     $this->assertEqual(20, count($collection->to('array')));
     $filter = function ($item) {
         return $item == 1;
     };
     $result = $collection->find($filter);
     $this->assertTrue($result instanceof Collection);
     $this->assertEqual(array_fill(0, 10, 1), $result->to('array'));
     $result = $collection->find($filter, array('collect' => false));
     $this->assertEqual(array_fill(0, 10, 1), $result);
 }
All Usage Examples Of lithium\util\Collection::find