<?php
function getPath(array $arr, string $path, mixed $default = null): mixed {
$cursor = $arr;
foreach (explode('.', $path) as $seg) {
if (!is_array($cursor) || !array_key_exists($seg, $cursor)) return $default;
$cursor = $cursor[$seg];
}
return $cursor;
}
$data = ['user' => ['profile' => ['email' => 'a@x.com']]];
echo getPath($data, 'user.profile.email'); // a@x.com
echo getPath($data, 'user.profile.phone', 'unknown'); // unknown
echo getPath($data, 'foo.bar.baz', 'fallback'); // fallback
Create a free account and build your private vault. Share publicly whenever you want.