我要转换此字符串数组;
$arList = ["the","quick","brown","fox"];
转换为这种格式。
[
"the" => [
"quick" => [
"brown" => []
]
]
]
对不起,您未发布一些代码。
这是我尝试过的,
<?php
$arList = ["the","quick","brown","fox"];
$newList = [];
$pointer = $newList;
foreach($arList as $item) {
$pointer[$item] = [];
$pointer = &$newList[$item];
}
echo "<pre>";
print_r($newList);
echo "</pre>";
答案 0 :(得分:0)
我使用以下代码从网上找到了解决方案;
$result = array_reduce(array_reverse($arList), function($prevArray, $key){
return $prevArray ? [$key => $prevArray] : [$key];
}, null);