简体   繁体   English

从嵌套关联数组中获取单个值

[英]Get a single value from a nested associative array

For a setup script I write in PHP (for CLI, not web) I use a settings file as well as other other "content" files. 对于我用PHP(对于CLI,不是Web)编写的安装脚本,我使用了设置文件以及其他“内容”文件。

The settings file is built up like a regular ini-file 设置文件的构建类似于常规的ini文件

[header.subheader.moreheaders...]
keyA = value
keyB = value1|value2|...

"header.subheader.moreheaders..." and "keyX" will form a nested associative array with "value" as a string or "value1|value2|..." as a simple array (0-...). “ header.subheader.moreheaders ...”和“ keyX”将形成一个嵌套的关联数组,其中“ value”为字符串或“ value1 | value2 | ...”为一个简单数组(0 -...)。

Thanks to this accepted answer , I got so far that I can split the headers into a recursive array recursively. 多亏了这个公认的答案 ,到目前为止,我可以将标头递归拆分为递归数组。 So far, so good. 到现在为止还挺好。

However, as the content files shall contain references to these variables, I would like to be able to read out single values from that multi-dimensional array with string placeholders like $@R[header.subheader.moreheaders.key] or $@R[header.subheader.moreheaders.key.0] depending on them being a string or an array. 但是,由于内容文件将包含对这些变量的引用,因此我希望能够使用$@R[header.subheader.moreheaders.key]$@R[header.subheader.moreheaders.key.0]类的字符串占位符从多维数组中读取单个值。 $@R[header.subheader.moreheaders.key.0]取决于它们是字符串还是数组。

In the script, $@R[header.subheader.moreheaders.key.0] should convert into $SettingsVar[header][subheader][moreheaders][key][0] to return the appropriate value. 在脚本中, $@R[header.subheader.moreheaders.key.0]应转换为$SettingsVar[header][subheader][moreheaders][key][0] $@R[header.subheader.moreheaders.key.0] $SettingsVar[header][subheader][moreheaders][key][0]以返回适当的值。

Neither the script nor the content files will know what is inside the settings file. 脚本和内容文件都不知道设置文件中的内容。 The script just knows the general structure and placeholder $@R[...] . 该脚本只知道常规结构和占位符$@R[...]

This answer appears to know what value will be in order to search for it. 这个答案似乎知道要搜索什么值。

Since I do not fully understand this answer , I am not sure if that would be the right way. 由于我不完全理解此答案 ,因此我不确定这是否是正确的方法。

Is there a similar easy way to get the reverse from building that array ? 有没有类似的简单方法可以从构建该数组中得到相反的结果?

After some contemplation, I found a decent enough solution, which works for me (and hopefully others): 经过深思熟虑,我找到了一个不错的解决方案,该解决方案对我(并希望对其他人)有效:

function GetNestedValue($aNestedKeys, $aNestedArray)
{
    $vValue = $aNestedArray;
    for($i = 0; $i < count($aNestedKeys); $i++)
    {
        if(array_key_exists($aNestedKeys[$i], $vValue))
        {
            $vValue = $vValue[$aNestedKeys[$i]];
        }
        else
        {
            $vValue = null;
            break;
        }
    }

    return $vValue;
}

Depending on what $aNestedKeys contain, it will either return a sub-array from $aNestedArray , a single value from it or null if any of the specified keys were not found. 根据$aNestedKeys包含的内容,它将从$aNestedArray返回一个子数组,从中返回一个单个值;如果未找到任何指定的键,则返回null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM