Yosymfony\Spress\Core\Support\ArrayWrapper::where PHP Method

where() public method

Filter using the given callback function.
public where ( callable $filter ) : array
$filter callable The filter function should be a function with the following signature: ```php function($key, $value) { // returns true if the value is matching your criteria. } ```
return array
    public function where(callable $filter)
    {
        $filtered = [];
        foreach ($this->array as $key => $value) {
            if ($filter($key, $value) === true) {
                $filtered[$key] = $value;
            }
        }
        return $filtered;
    }

Usage Example

Example #1
0
 public function testWhere()
 {
     $data = [];
     $a = new ArrayWrapper(['value1' => 1, 'value2' => 2]);
     $filtered = $a->where(function ($key, $value) {
         return $value > 1;
     });
     $this->assertCount(1, $filtered);
 }