Re: RFC [Discussion]: array_find
| From: | Joshua Rüsweg | Date: | Fri, 19 Apr 2024 19:34:42 +0000 |
| Subject: | Re: RFC [Discussion]: array_find | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-123161@lists.php.net to get a copy of this message | ||
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