cha*_*er2 2 php arrays associative-array
我需要像这样创建数组:
Array('firstkey' => Array('secondkey' => Array('nkey' => ...)))
Run Code Online (Sandbox Code Playgroud)
由此:
firstkey.secondkey.nkey.(...)
Run Code Online (Sandbox Code Playgroud)
Lek*_*eyn 12
$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
Run Code Online (Sandbox Code Playgroud)