我有一个这样的数组:
[0] => [basketball][player]
[1] => [basketball][threes][player]
[2] => [basketball][home][leaders][player]
[3] => [basketball][away][leaders][player]
我想将每个元素转换为对关联数组中某个元素的引用:
$post['basketball']['player']
$post['basketball']['threes']['player']
etc.
有没有办法自动将前者翻译成后者?如果是这样的话会非常方便,如果没有,可能会非常不方便,所以我希望有办法,但我不知道它会是什么。
答案 0 :(得分:2)
我会这样说:
[
,在结束时剥离]
(使用substr
)explode
将字符串拆分为][
这样的事情:
$array = ..... // the big array
$str = "[basketball][player]";
$keys = explode("][",substr($str,1,-1));
$pos = $array; // PHP does a lazy copy, so there is no performance issue here
while($key = array_shift($keys)) $pos = $pos[$key];
// $pos is now your target element