为了得到数组元素分析串 [英] Parse string in order to get array elements
问题描述
我试图找出如何采取一个字符串,分析它,并获得相匹配的解析钥匙还给了相应的数组值。我不知道如何正确地解释这一点,所以这里是一个例子。
I'm trying to figure out how to take a string, parse it, and get the corresponding array values that match the parsed keys back. I don't know how to properly explain this, so here's an example.
$testArray = array(
'router' => array(
'format' => 'xml',
),
);
我想分析看起来像这样的字符串 - router.format
,而我显然并不期待回到 XML
为字符串。传入的String可以分离期间的任意金额。我想以某种方式做递归的,但我想不出任何方式在此会工作。
The string I want to parse will look like this - router.format
, and I'm obviously expecting back xml
as a string. The passed in string can have any arbitrary amount of separating periods. I was thinking of doing recursion somehow, but I can't think of any way in which this will work.
这是非常类似于SF2的服务容器,并在你的服务返回的路上。
This is very similar to SF2's service container and the way in which you get services back.
任何帮助将是巨大的!
推荐答案
可能需要一些错误检查:
Might need some error checking:
$testArray = array(
'router' => array(
'format' => 'xml',
),
);
$path = 'router.format';
$result = $testArray;
foreach(explode('.', $path) as $key) {
if(isset($result[$key])) {
$result = $result[$key];
}
}
print_r($result);
这篇关于为了得到数组元素分析串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!