Re: [RFC] [Discussion] array_get and array_has functions

From: Date: Sun, 12 Apr 2026 14:41:22 +0000
Subject: Re: [RFC] [Discussion] array_get and array_has functions
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-130609@lists.php.net to get a copy of this message
Hi Am 2026-04-04 16:06, schrieb Barel:
This is the link to the RFC: https://wiki.php.net/rfc/array_get_and_array_has
Thank you for your RFC. I find it not obvious at all that: - Non-(string|int) path segments will result in the default value being returned, rather than a TypeError being thrown or just proceeding with implicit coercion. - Encountering a non-array value along the path will result in the default value being returned, rather than an Exception being thrown. Returning a default value for an existing path element that is not an array feels like a source of error, particularly when any intermediate value is an object implementing ArrayAccess. I would expect the following to hold:
    $path = ['foo', 'bar', 'baz'];
    array_get($array, $path) === array_get(array_get($array, array_shift($path), []), $path);
which it will not due to (2). And the further looking at the examples:
    $array = /* … */;
    array_has($array, ['product', 'name']);
    array_has($array, ['product', 'color']);
Just looking at the code without knowing what the function does, I probably would have expected it to be either: 1. in_array() operating on multiple values that all need to be there. 2. array_key_exists() operating on multiple keys that all need to be there. But not a “path through the array”. Best regards Tim Düsterhus

« previous php.internals (#130609) next »