Supposing that $jsons is an array of objects, and that each object has a description property in which you would search
$items = new Collection($jsons);
$results = $items->filter(function($item) use ($search) {
return strpos($item->description, $search);
})->get();
longilineo said:
Supposing that $jsons is an array of objects, and that each object has a description property in which you would search
$items = new Collection($jsons); $results = $items->filter(function($item) use ($search) { return strpos($item->description, $search); })->get();
Wonderful solution, Thank you for your help. I'm going to give this one a try, Let you know.
longilineo, Can I do anything like this.
$results = $items->filter(function($item) use ($search)
{
return strpos(null, array(
$item->moduleCode,$search,
$item->staffmember,$search
));
$data = array_merge($results);
})->get();
If it's a question you're using strpos in a completely wrong manner: http://php.net/manual/it/function.strpos.php
Everything you're doing in the function after return will never execute.
filter method add an $item to $results only if returned condition is true.
If $search is equal to "bar", my example take all $items in which description is something like "every thing else bar every thing else".
If I understand you need all items in which moduleCode OR staffmember contain $search. So the code should be:
$items = new Collection($jsons);
$results = $items->filter(function($item) use ($search) {
return strpos($item->moduleCode, $search) ||
strpos($item->staffmember, $search) ;
})->get();
Thank you for reply.
One more question.
Can I add tags and "like" to my query search function.
('title', 'LIKE', '%' . $search . '%')
Your datasource isn't a relational database so you can't make a query with sql syntax and you can't use Eloquent.
However with my code you are doing something like:
select * from results where moduleCode like "%$search%" or staffmember like "%$search%";
But you can't use this syntax.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community