我试图通过动态名称分配数组的值我正在尝试跟随
$arr = array();
$path = 'arr'."['item']['abc']";
${$path} = array(
'name'=>'somename',
'other'=>'...'
);
不是${$path} => $改编[ ‘项目’] [ ‘ABC’];
我也试过了$$路径,应该评价为$arr [‘item’] [‘abc’];但他们都没有工作
http://codepad.viper-7.com/Dc8Jei
更新 – > http://codepad.viper-7.com/k4sIgJ
(这是amazon aws s3对象键)
aaa/aaab/
aaa/a.png
aaa/abc/one.png
abb/a/
abb/some/
abb/some/ac.png
现在我要做的是将这些项目存储在数组中
我将这些键分解成这个字符串就像
['aaa']['abc']
现在我想得到的是
[aaa]{
[aaab]{}
[abc]{
[0]{one.png}
}
[aaab]{}
}
它只是为了说明
解决方法:
<?PHP
$arr = array();
$loc_arr = array(
'item' => array(
'abc',
'www',
'ccc'),
'other item' => array(
'other index'
)
);
foreach($loc_arr as $item => $scndLevel){
foreach ( $scndLevel as $level) {
$arr[$item][$level] = array(
'name'=>'somename',
'other'=>'...'
);
}
}
print_r($arr);
这打印:
Array
(
[item] => Array
(
[abc] => Array
(
[name] => somename
[other] => ...
)
[www] => Array
(
[name] => somename
[other] => ...
)
[ccc] => Array
(
[name] => somename
[other] => ...
)
)
[other item] => Array
(
[other index] => Array
(
[name] => somename
[other] => ...
)
)
)
更新:
伪代码:
dir = array();
function traverse(dir)
{
foreach ( direntry as filename) {
if ( filename is a sub directory ) {
dir[filename] = array();
traverse(dir[filename]);
} else {
dir[] = filename;
}
}
dir['/'] = array();
traverse(dir['/']);