按数组变量索引php数组
我有个小问题。我需要索引一个php数组的数组变量 例如:按数组变量索引php数组,php,arrays,Php,Arrays,我有个小问题。我需要索引一个php数组的数组变量 例如: $structure = []; $url = "/one/two/three"; $urlParts = explode("/", $url); // I need convert $urlParts -> to array index ["one"]["two"]["three"] // Expected result $structure["one"]["two"]["three"] = true; 在php语言中可能吗?
$structure = [];
$url = "/one/two/three";
$urlParts = explode("/", $url);
// I need convert $urlParts -> to array index ["one"]["two"]["three"]
// Expected result
$structure["one"]["two"]["three"] = true;
在php语言中可能吗?您可以使用reference获得如下结果:
<?php
$urlParts = ['one', 'two', 'three'];
$o = [];
$ref = &$o;
$len = count($urlParts);
foreach($urlParts as $k => $v)
{
$ref[$v] = [];
$ref = &$ref[$v];
}
$ref = true;
var_dump($o);
echo var_dump($o['one']['two']['three']);
您想使用
.htaccess
或PHP本身来实现这一点吗?变量$url就是一个例子。我需要它用于PHP中的类似用例。
ei@localhost:~$ php test.php
array(1) {
["one"]=>
array(1) {
["two"]=>
array(1) {
["three"]=>
&bool(true)
}
}
}
bool(true)