Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将字符串的平面数组转换为关联数组_Php - Fatal编程技术网

Php 将字符串的平面数组转换为关联数组

Php 将字符串的平面数组转换为关联数组,php,Php,我想转换这个字符串数组 $arList = ["the","quick","brown","fox"]; 转换成这种格式 [ "the" => [ "quick" => [ "brown" => [] ] ] ] 很抱歉没有发布一些代码 这是我试过的 <?php $arList = ["the","quick","brown","fox"]; $newList = []; $pointer = $

我想转换这个字符串数组

$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>";

我使用以下代码从网上找到了一个解决方案


请发布您的一些代码/努力,然后只有我们可以帮助您。福克斯发生了什么事?
$result = array_reduce(array_reverse($arList), function($prevArray, $key){
    return $prevArray ? [$key => $prevArray] : [$key];
}, null);