Skip to content
PHP News
Search
Declined PHP 8.6

array_path_get and array_path_exists functions

Adds array_path_get() and array_path_exists() to read or check a value deep in a nested array using an array of keys.

Implement array_path_get and array_path_exists functions as outlined in the RFC?

Primary vote · 2/3 majority

1 Yes 17 No 6 abstain 6% approval

This poll has closed.

It was declined 1 to 17, with 6 abstaining, far short of the two-thirds majority it needed. Voting opened on May 18, 2026, and closed on June 1, 2026.

Summary

Reading a value deep inside a nested array is straightforward when you know the keys ahead of time. It gets harder when the path comes from config or user input, because then you have to loop through the keys and check each level yourself. This RFC from Carlos Granados proposed two functions to handle that in one call: array_path_get() and array_path_exists().

Show me

When the keys are fixed, you can already write this:

$array = ['products' => ['desk' => ['price' => 100]]];

$price = $array['products']['desk']['price'] ?? null;

With the RFC, the path is an array you can build at runtime:

$price = array_path_get($array, ['products', 'desk', 'price']); // 100
$discount = array_path_get($array, ['products', 'desk', 'discount'], 10); // 10

array_path_exists($array, ['products', 'desk', 'price']); // true

Dot notation wasn't built in, but you could split a string into a path first:

$path = explode('.', 'products.desk.price');
$value = array_path_get($array, $path);

How it works

array_path_get(array $array, array $path, mixed $default = null): mixed
array_path_exists(array $array, array $path): bool
  • Each item in $path is one level. Items must be strings or ints. Anything else throws a TypeError, even if the lookup never reaches that level.
  • Int items work as list keys. So ['users', 0, 'name'] reads the first user's name.
  • Missing paths don't warn. array_path_get() returns $default, and array_path_exists() returns false.
  • Every level before the last must be an array. If one isn't, the lookup stops and treats the path as missing.
  • null still counts as existing. Like array_key_exists(), a key set to null makes array_path_exists() return true.

The RFC points to Laravel's Arr::get() and Arr::has(), and to Lodash's get() and has() in JavaScript, as similar helpers. Wildcards like ['products', '*', 'price'] were left for a future proposal.

The design changed a lot during discussion. Early versions took a dot-notation key and used different names, while the final version accepts only an array path.

What it means for existing code

Nothing would break, since the RFC only adds two new global functions. A GitHub search found no public code already using these names.