Q:

Alternative to ->where() for Arrays in PHP/Laravel

I made a function that gets all of the tickets from the Teamwork Desk API. I want to filter out the tickets by date created, but can't use ->where() on an array. Is there an alternative?
Like I've stated before I've tried ->where(), googling didn't help me.
class ApiController extends Controller
{
public function getTickets() {
    $client = new \GuzzleHttp\Client([
        'base_uri' => config('teamwork.apilink'),
        'auth' => [config('teamwork.apikey'), 'x'],
        'verify' => false
    ]);

    $response = $client->get('v1/tickets/search.json', [
        'query' => ['sortDir' => 'desc', 'sortBy' => 'updatedAt']
    ])->where('id', 'LIKE', '4477011');

    $json = (string)$response->getBody()->getContents();
    dd(json_decode($json, true));
}
}

Without the where it pastes all the tickets, with the "where" it just errors = 'Call to undefined method GuzzleHttp\Psr7\Response::where()'

A:

There are multiple solution to get this done. You could use a Laravel Collection for this.
$tickets = collect(json_decode($response->getBody()->getContents(), true));

$tickets = $tickets->filter(function($ticket) {
    return $ticket['createdAt'] >= now();
});

Or like Script47 suggested in the comments you could also use the array_filter method (which Laravel also uses under the hood):
$tickets = array_filter($tickets, function($ticket) {
    return $ticket['createdAt'] > now();
});

The benefit of using Collections from Laravel is that Laravel provides a lot of methods to make your life easier, however the downside is that in some cases it could be slower. This really depends on what exactly you're trying to accomplish. Personally I like to use the Collection, it looks a little bit cleaner (imo) and saves me some time.

A:

First, you need to extract the data from your request:
$response = $client->get('v1/tickets/search.json', [
    'query' => ['sortDir' => 'desc', 'sortBy' => 'updatedAt']
])->where('id', 'LIKE', '4477011');

$json = $response->getBody()->getContents();

Then you can create a collection from the response array:
$json = json_decode($json, true);
$collection = collect($json['items']);

Now you can filter the collection, using the methods described in the official documentation I linked you above:
$collection = $collection->where('id','4477011');

All available methods: https://laravel.com/docs/5.8/collections#available-methods
Note that this way you will download all the tickets from the teamwork api, and you are filtering the result on your application. If you want to only receive the filtered tickets, you may need to read more carefully the API documentation and create the request accordingly.

