I have the following function:
public static function toArray($key, $value) {
$e = explode('.', $key);
$return = array();
for($i = count($e) - 1; $i >= 0; $i--) {
$return = array($e[$i] => $return);
}
return $return;
}
and the output:
Array
(
[app] => Array
(
[shifts] => Array
(
[total] => Array
(
)
)
)
)
And desired output:
Array
(
[app] => Array
(
[shifts] => Array
(
[total] => 3
)
)
)
What happens here is that a string is given and and creates a multi-dimensional array using the dot as the delimiter. However, I wish to assign the last array a value given by the function. This example is assuming that the string input is: "apps.shifts.total" with a value of 3. I'm trying to find an elegant way to achieve this without going into useless loops. Any inspiration would be much appreciated!