luya\helpers\ArrayHelper::search PHP Method

    public static function search($array, $searchText, $sensitive = false)
    {
        $function = $sensitive ? 'strpos' : 'stripos';
        return array_filter($array, function ($item) use($searchText, $function) {
            $response = false;
            foreach ($item as $key => $value) {
                if ($response) {
                    continue;
                }
                if ($function($value, "{$searchText}") !== false) {
                    $response = true;
                }
            }
            return $response;
        });
    }

Usage Example

Ejemplo n.º 1
0
 public function testSearch()
 {
     $data = [['name' => 'Foo Bar', 'description' => 'same', 'id' => 1], ['name' => 'Baz foo', 'description' => 'same', 'id' => 2]];
     $this->assertSame(1, count(ArrayHelper::search($data, '1')));
     $this->assertSame(1, count(ArrayHelper::search($data, 1)));
     $this->assertSame(2, count(ArrayHelper::search($data, 'FOO')));
     $this->assertSame(2, count(ArrayHelper::search($data, 'foo')));
     $this->assertSame(2, count(ArrayHelper::search($data, 'Foo')));
     $this->assertSame(2, count(ArrayHelper::search($data, 'fo')));
     $this->assertSame(1, count(ArrayHelper::search($data, 'Foo', true)));
 }