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.
I have another solution based on eval I just concatenate the strings with brackets around
Use a reference to refer to the latest known point in the array and update it with each step:
The
if
branches depend on the operation. Here’s an example for the getter and setter:As you can see here, the function
explode
takes the string and returns an array. So here is what you need to do:And for the get: