How can nested arrays be created from a string using PHP?

To create nested arrays from a string in PHP, you can use functions like explode() to split the string into an array and then loop through the array to build the nested structure. You can use a delimiter to determine the hierarchy of the nested arrays. By splitting the string based on the delimiter and organizing the elements into nested arrays, you can create a hierarchical structure.

$string = "first.second.third.fourth";
$delimiter = ".";
$nestedArray = [];

$keys = explode($delimiter, $string);
$currentArray = &$nestedArray;

foreach ($keys as $key) {
    $currentArray[$key] = [];
    $currentArray = &$currentArray[$key];
}

print_r($nestedArray);