使用点语法从多维数组中获取值 [英] Get a value from a multidimensional array using the dot syntax

查看:92
本文介绍了使用点语法从多维数组中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数组:

$conf = array(
'db'    => array(
    'server'    =>  'localhost',
    'user'      =>  'root',
    'pass'      =>  'root',
    'name'      =>  'db',
    ),
'path'  =>  array(
    'site_url'  =>  $_SERVER['SERVER_NAME'],
    'site_dir'  => CMS,
    'admin_url' => conf('path.site_url') . '/admin',
    'admin_dir' => conf('path.site_dir') . DS .'admin',
    'admin_paths' => array(
                'assets' => 'path'
                ),
    ),
);

我想使用如下函数从该数组中获取一个值:

I would like to get a value from this array using a function like so:

/**
 * Return or set a configuration setting from the array.
 * @example
 * conf('db.server')    => $conf['db']['server']
 *
 * @param  string $section the section to return the setting from.
 * @param  string $setting the setting name to return.
 * @return mixed           the value of the setting returned.
 */
function conf($path, $value = null) {
    global $conf;
    // We split each word seperated by a dot character
    $paths = explode('.', $path);

    return $conf[$paths[0]][$paths[1]];
}

但是如果函数能够解析数组的所有维,而不仅仅是前两个维,我会很喜欢.

But i would like it if the function resolves all dimensions of the array and not just the first two.

conf('path.admin_paths.assets'); 

将决定

=> $conf['path']['admin_paths']['assets']

我该怎么做?另外,如果该函数具有另一个参数,我将如何设置它,而不是返回一个值?

How would i do this? Also, how would i make this function if it has another param, would set a value rather than return it?

推荐答案

我发现@Danijel递归方法比最初尝试的方法干净得多.因此,这是该功能的递归实现,支持设置值.

I found @Danijel recursive approach a lot cleaner than my initial try. So here's a recursive implementation of the functionality, supporting setting values.

function array_get_by_key(&$array, $key, $value = null) {
    list($index, $key) = explode('.', $key, 2);
    if (!isset($array[$index])) throw new Exception("No such key: " . $index);

    if(strlen($key) > 0)
        return array_get_by_key(&$array[$index], $key, $value);

    $old = $array[$index];
    if ($value !== null) $array[$index] = $value;
    return $old;    
}

function config($key, $value = null) {
    global $CONFIG;
    return array_get_by_key(&$CONFIG, $key, $value);
}

试运行:

$CONFIG = array(
'db'    => array(
    'server'    =>  'localhost',
    'user'      =>  'root',
    'pass'      =>  'root',
    'name'      =>  'db',
    ),
'path'  =>  array(
    'site_url'  =>  'localhost',
    'site_dir'  => 'CMS',
    'admin_url' => 'localhost/admin',
    'admin_dir' => 'localhost/res/admin',
    'admin_paths' => array(
                'assets' => 'path'
                ),
    ),
);

try {
    var_dump(config('db.pass'));
    var_dump(config('path.admin_url', 'localhost/master'));
    var_dump(config('path.admin_url'));
    var_dump(config('path.no_such'));
} catch (Exception $e) {
    echo "Error: trying to access unknown config";
}

// string(4) "root"
// string(15) "localhost/admin"
// string(16) "localhost/master"
// Error: trying to access unknown config

这篇关于使用点语法从多维数组中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆