Hi

On 10.04.24 00:12, Larry Garfield wrote:
Another alternative is to always return the key, because you can trivially get 
the value from the key, but not vice versa.  Of course, the resulting syntax 
for that is frequently fugly.

$val = $array[array_find($db, $array)] ?? some-default;


In 95% (roughly) of cases, I (personally) need the array value and not the key. Just having the option to get the key is already a step forward compared to the current state, but I personally would find it impractical (and fugly, as you said) to use the method, especially if the search callback is more complex and multi-line (and it is fugly, as you said).

Here is an example for this, without using a helper variable:

$value = $array[\array_find($array, function ($value, $key): bool {
    if ($key % 5) {
        return \strlen($value) < 100;
    }

    if ($key % 2) {
        return \strlen($value) < 40;
    }

    return false;
})];

So I think, this is not an option.

Cheers

Josh

Reply via email to