我有一個字符串(如one.two.three
或month.2
),我需要將此字符串爆炸點字符轉換爲數組鍵。將點分成數組鍵的字符串
所以我month.2
,我需要的代碼行添加到該字符串翻譯成$lang['month'][2]
我一直在尋找解決方案,但我不能找到它,我阻止。
我終於發現了這一點:
<?php
$lang['one']['two']['three'] = 'well done';
$str = 'one.two.three';
$list = explode('.', $str);
$result = '$lang';
foreach ($list as $item)
{
$result .= '["'.$item.'"]';
}
var_dump(eval("return " . $result.';'));
?>
請考慮回答自己的問題,而不是編輯問題的答案。你甚至可以接受它。 **注意**:我強烈建議不要使用這種「eval」解決方案。只有在沒有其他方式時才應該使用'eval'(幾乎從不)。 – kapa 2011-06-08 06:51:07