<?php
function setPath(array &$arr, string $path, mixed $value): void {
$cursor = &$arr;
$segs = explode('.', $path);
$last = array_pop($segs);
foreach ($segs as $seg) {
if (!isset($cursor[$seg]) || !is_array($cursor[$seg])) {
$cursor[$seg] = [];
}
$cursor = &$cursor[$seg];
}
$cursor[$last] = $value;
}
$state = [];
setPath($state, 'user.profile.email', 'a@x.com');
setPath($state, 'user.profile.name', 'Alice');
print_r($state);
// [ user => [ profile => [ email => a@x.com, name => Alice ] ] ]
Create a free account and build your private vault. Share publicly whenever you want.