获取/设置多维数组数据的函数,如getData('my.multi.array') [英] Function to get/set multidimensional array data like getData('my.multi.array')

查看:98
本文介绍了获取/设置多维数组数据的函数,如getData('my.multi.array')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 类foo 
{
private $ config = array();

public __construct(){
$ this-> config ['site'] ['title'] ='XXXXX';
$ this-> config ['site'] ['favicon'] ='XXXXX.ico';
$ this-> config ['site'] ['keywords'] = array('page','cute','other');
$ this-> config ['otherthing'] ='value';
/ ** etc ** /
}

公共函数集($ name,$ value)
{
$ tmp = explode('。 ,$名);
/ **我不知道我可以在这里做什么** /
}

public function get($ name)
{
$ tmp =爆炸(,$名 '');
/ **我不知道我可以在这里做什么** /
}
}

$ thing = new foo;
$ this-> set('site.keywords',array('other','keywords')); //这改变了foo-> config中的数据['site'] ['keywords' ]

echo $ this-> get('site.title'); //获取foo-> config中的数据['site'] ['title']
echo $ this-> get('otherthing'); //获取数据在foo->配置中['otherthing']

数组维度可以在dinamically我想在foo-> config中设置/检索数据,方法如下:function(fist.second.third.four.etc)。



编辑:



我可以用explode创建一个函数,我探索了这种可能性,但是如果我使用这样的函数:

函数get($ name)
{
$ tmp = explode('。',$ name);
if(isset($ this-> config [$ tmp [0]] [$ tmp [1]]))
return $ this-> config [$ tmp [0]] [$ TMP [1]];
return'';

$ / code>

当我需要获取三维数组的值($ this- > config [one] [two] [tree])或一个维度($ this-> config [one])函数不能处理结果。
我想获得N维的数组。

我也试过这个解决方案:
函数nameToArray($ name)
{
$ tmp = explode('。',$ name);
$ return ='';

  foreach($ tmp as $ v)
{
$ return。=['{$ v}'];

}

return'config'。$ return;
}
函数集($ name,$ value)
{
$ this-> {$ this-> nameToArray} = $ value;
}

$ foo-> set('site.favicon','something.ico');

但是这不会编辑$ foo-> config中的数组,这会在$ this内创建一个新值所谓的字面配置['网站'] ['favicon']。

我不知道我能做到这一点,即时通讯尝试了很多方法,但我不能得到预期的结果。
感谢您的帮助。

解决方案

使用 reference 来引用数组中的最新已知点并在每一步中更新它:

  $ ref =& $ this-> config; 
$ keys = explode('。',$ name);
foreach($ key为$ idx => $ key){
if(!is_array($ ref)){
//引用不引用数组
/ /这是一个问题,因为如果(!array_key_exists($ key,$ ref)){
// key不存在$ b $,我们仍然有至少一个键去
}
b //反应取决于操作
}
//更新引用
$ ref =& $ ref [$ key];

$ / code>

if 分支取决于操作。下面是getter和setter的例子:

  public function get($ name){
$ ref =& $这 - >配置;
$ keys = explode('。',$ name);
foreach($ keys as $ idx => $ key){
if(!is_array($ ref)){
抛出新异常('key''.implode('。') ,array_slice($ keys,0,$ idx-1))。'不是数组'';

if(!array_key_exists($ key,$ ref)){
throw new Exception('key''.implode('。',array_slice($ keys,0,$ idx ))。' 不存在');
}
$ ref =& $ ref [$ key];
}
return $ ref;
}
公共函数集($ name,$ value){
$ ref =& $ this-> config;
$ keys = explode('。',$ name);
foreach($ keys as $ idx => $ key){
if(!is_array($ ref)){
抛出新异常('key''.implode('。') ,array_slice($ keys,0,$ idx))。'不是数组,而是'.gettype($ ref));

if(!array_key_exists($ key,$ ref)){
$ ref [$ key] = array();
}
$ ref =& $ ref [$ key];
}
$ ref = $ value;
}


im trying to get dinamically a data inside a class like this:

Class foo
{
    private $config=array();

    public __construct(){    
        $this->config['site']['title'] = 'XXXXX';
        $this->config['site']['favicon'] = 'XXXXX.ico';
        $this->config['site']['keywords'] = array('page','cute','other');        
        $this->config['otherthing'] = 'value';
        /**etc**/
    }

    public function set($name,$value)
    {
        $tmp = explode('.',$name);
        /** i dont know what i can do here **/
    }

    public function get($name)
    {
        $tmp = explode('.',$name);
        /** i dont know what i can do here **/
    }
}

$thing = new foo;
$this->set('site.keywords',array('other','keywords'));//this change a data inside foo->config['site']['keywords']

echo $this->get('site.title'); // gets data inside foo->config['site']['title']
echo $this->get('otherthing'); // gets data inside foo->config['otherthing']

Array dimensions can be change dinamically and i want set/retrieve data in foo->config calling the array by the way : function(fist.second.third.four.etc).

Edit:

I can create a function with explode, i explored that posibility, but if i use a function like this:

function get($name)
{
    $tmp = explode('.',$name);
    if(isset($this->config[$tmp[0]][$tmp[1]]))
        return $this->config[$tmp[0]][$tmp[1]];
    return '';
}

When i need to take a value of array in 3 dimensions($this->config[one][two][tree]) or one dimension ($this->config[one]) the function cant handle the result. I want to get N dimensions of array.

I tried too this solution: function nameToArray($name) { $tmp = explode('.',$name); $return = '';

    foreach($tmp as $v)
    {
        $return .= "['{$v}']";

    }

    return 'config'.$return;
}
function set($name,$value)
{
    $this->{$this->nameToArray} = $value;
}

$foo->set('site.favicon','something.ico');

But this doesnt edit a array inside $foo->config, this creates a new value inside $this called literally config['site']['favicon'].

I dont know how i can do it, im tried many ways but i cant got the spected result. Thanks for help.

解决方案

Use a reference to refer to the latest known point in the array and update it with each step:

$ref = &$this->config;
$keys = explode('.', $name);
foreach ($keys as $idx => $key) {
    if (!is_array($ref)) {
        // reference does not refer to an array
        // this is a problem as we still have at least one key to go
    }
    if (!array_key_exists($key, $ref)) {
        // key does not exist
        // reaction depends on the operation
    }
    // update reference
    $ref = &$ref[$key];
}

The if branches depend on the operation. Here’s an example for the getter and setter:

public function get($name) {
    $ref = &$this->config;
    $keys = explode('.', $name);
    foreach ($keys as $idx => $key) {
        if (!is_array($ref)) {
            throw new Exception('key "'.implode('.', array_slice($keys, 0, $idx-1)).'" is not an array');
        }
        if (!array_key_exists($key, $ref)) {
            throw new Exception('key "'.implode('.', array_slice($keys, 0, $idx)).'" does not exist');
        }
        $ref = &$ref[$key];
    }
    return $ref;
}
public function set($name, $value) {
    $ref = &$this->config;
    $keys = explode('.', $name);
    foreach ($keys as $idx => $key) {
        if (!is_array($ref)) {
            throw new Exception('key "'.implode('.', array_slice($keys, 0, $idx)).'" is not an array but '.gettype($ref));
        }
        if (!array_key_exists($key, $ref)) {
            $ref[$key] = array();
        }
        $ref = &$ref[$key];
    }
    $ref = $value;
}

这篇关于获取/设置多维数组数据的函数,如getData('my.multi.array')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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