On Fri, Apr 19, 2024, at 7:34 PM, Joshua Rüsweg wrote: > 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
I think at this point I'm on team "let's just make it 2 separate functions, give them good names, and move on with life." There's another issue, though: Will the callback always be given both $value and $key? If so, it's incompatible with internal single-parameter functions. If not, and it tries to auto-detect, we run into issues with functions with optional second arguments. (This is a pre-existing mess I've run into before.) --Larry Garfield