Fresh code drop

Find the first matching array value

PHP

PHP 8.4 tip: use array_find() instead of a manual search loop. Source: php.net/releases/8.4

PHP
CodeSnap // free canvas

Find the first matching array value

<?php

$admin = array_find(
    $users,
    static fn (User $user): bool => $user->isAdmin(),
);

// $admin is the first match, or null.

Turn your code into a post.Five templates are free—no account needed.

Remix this snap

Community remixes

Hydrate eager-loaded parents on children