Re: RFC karma request: array_partition()

From: Date: Sat, 24 Oct 2026 13:18:40 +0000
Subject: Re: RFC karma request: array_partition()
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-132914@lists.php.net to get a copy of this message
Hi Morgan, Thanks for the support! > my partition functions also map passes and rejects to true and false > respectively (so the result is a mapping from booleans to array values). > > [true => $passed, false => $rejected] = array_partition(...); I looked at this, and the catch is that boolean array keys are cast to integers, so true => 1 and false => 0. A boolean-keyed result would therefore have to put the rejected elements at index 0, and then positional destructuring silently gives the opposite of what it reads: [$adults, $minors] = array_partition($users, fn($u) => $u->age >= 18); // with a bool-keyed result, $adults would hold the minors Whichever layout is chosen, one of the two idioms ends up swapped. I'd rather keep "matching first" at index 0, since that's what Ruby, Kotlin, Rust, Haskell and Laravel all return, and it makes the plain [$matching, $rest] destructuring work. I'll document the pitfall explicitly in the RFC, with your example, so nobody reaches for [true => ..., false => ...] expecting it to work. > I would also prefer the callback parameter to be named $predicate, to > emphasise that it is supposed to return a boolean. I agree it's more descriptive in isolation. But every other array function taking a callback names it $callback, including array_filter() and the recent array_find(), array_find_key(), array_any() and array_all(), which are also predicates. With named arguments I think it's more valuable that callback: works the same way across the whole family, so I'd prefer to keep $callback for consistency. I'll mention it in the RFC as a considered alternative. Thanks, Ayoub On Sat, Oct 24, 2026 at 4:47 AM Morgan <weedpacket@varteg.nz> wrote: > > On 2026-10-24 12:16, mickmackusa wrote: > > Hi Ayoub, > > > > - It feels unintuitive that true callback outcomes map to > > the 0 key > > and false outcomes map to > > 1. > I'm also in support, and this would have been my only observation: my > partition functions also map passes and rejects to true and false > respectively (so the result is a mapping from booleans to array values). > > If that mapping were retained, it could be documented in code as > > [true => $passed, false => $rejected] = array_partition(...); > > or > > $adult_tested = array_partition(...); > > $adults = $adult_tested[true]; > $minors = $adult_tested[false]; > > > > > > > I would also prefer the callback parameter to be named $predicate, to > emphasise that it is supposed to return a boolean. $callback doesn't > convey as much about what it's for - the same name is used, for example, > in usort(), where it's a comparator. It didn't matter until named > parameters became a thing. >

« previous php.internals (#132914) next »