gpt4 book ai didi

php - 从字符串创建数组

转载 作者:行者123 更新时间:2023-12-04 20:14:38 27 4
gpt4 key购买 nike

我需要创建这样的数组:

Array('firstkey' => Array('secondkey' => Array('nkey' => ...)))

来自这里:

firstkey.secondkey.nkey.(...)

最佳答案

$yourString = 'firstkey.secondkey.nkey';
// split the string into pieces
$pieces = explode('.', $yourString);
$result = array();
// $current is a reference to the array in which new elements should be added
$current = &$result;
foreach($pieces as $key){
// add an empty array to the current array
$current[ $key ] = array();
// descend into the new array
$current = &$current[ $key ];
}
//$result contains the array you want

关于php - 从字符串创建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3793448/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com