2

I have the following array:

$elements = collect([
     ['product_id' => 'prod-100', 'name' => 'Desk'],
     ['product_id' => 'prod-200', 'name' => 'Chair'],
 ]);

I need to filter by approximation, but just using the collection data, something like:

$elements->where('name', 'LIKE', 'De%')->values()->all();

Actually i can filter the data, but with a normal filter (where), this does not work for me because it find the exact coincidences. So, if i use a normal filter i have to say the exactly value to match:

$elements->where('name', 'Desk');

How i can made a query to the array data using something like a "Where LIKE" clausule?

Simón Farias
  • 732
  • 2
  • 8
  • 21

1 Answers1

3

You can use filter function in collection and in closure use preg_match php function to check if it exist in name parameter like this:

$name='de';
$elements->filter(function ($item) use($name){
    return preg_match("/$name/",$item['name']);
});
dWinder
  • 11,597
  • 3
  • 24
  • 39
Mohammed Aktaa
  • 1,345
  • 9
  • 14