@eristic You're going to need to provide a bit more information. For example, what do these events look like? What do the filters look like? What do you want to filter by, etc.
Filter through an array (not Eloquent model)
I'm building an array for an API, but I'm having difficulty figuring out how to filter the results of that array.
Is there a way to filter an array so that it returns all the data associated with the filter and not just the filter? I've tried array_filter() to no avail.
public function getFilteredEvents(Request $request)
{
$events = $this->event->generateAllEvents();
if($request->has('primary_type')) {
return $request->input('primary_type');
}
}
$events is the array and I'm grabbing the primary_type from a query string. I have no idea how to associate the $event data with the request. Right now I'm just returning a string.
Thanks in advanced.
For sure, this could totally be done with array_filter, you might have run into slight trouble because it looks like you're filtering on an array of an array of events (unless that was a typo), but be sure to check out that you're filtering a single array of events.
Ex. (Using PHP arrays. Notice it isn't 3 levels deep)
$events = [
[
'event_id' => 1,
],
[
'event_id' => 2,
],
]
Either way, after you have your correct array of events, you could filter them like this.
$type = $request->input('primary_type');
$events = array_filter($events, function ($event) use ($type) {
return $event['primary_type'] === $type;
});
That will return an array of event arrays where the primary_type matches the provided input. Hope that helps you out.
Please or to participate in this conversation.