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;
}
I have another solution based on eval
I just concatenate the strings with brackets around
<?php
Class foo {
private $config=array();
function __construct(){
$this->config['site']['title'] = 'testing';
$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);
$array_levels = "['";
foreach($tmp as $key) {
$array_levels .= $key."']['";
}
$array_levels = substr($array_levels, 0, -2);
eval('$this->config'.$array_levels.' = $value;');
}
public function get($name)
{
$tmp = explode('.',$name);
$array_levels = "['";
foreach($tmp as $key) {
$array_levels .= $key."']['";
}
$array_levels = substr($array_levels, 0, -2);
eval('$value = $this->config'.$array_levels.';');
return $value;
}
}
$thing = new foo;
$thing->set('site.keywords',array('other','keywords'));
echo $thing->get('site.title');
As you can see here, the function explode
takes the string and returns an array. So here is what you need to do:
public function set($name,$value)
{
$tmp = explode('.',$name); //$tmp will now contain an array, so if $name = 'site.keywords'
//$tmp[0] will contain 'site' and $tmp[1] = 'keywords'
//if you send 'otherthing' as $name, then $tmp[1] will have no value
if(isset($tmp[1])){
$this->config[$tmp[0]][$tmp[1]] = $value; // this is like saying $this-config['site']['keywords'] = $value
}
else{
$this->config[$tmp[0]] = $value;
}
}
And for the get:
public function get($name)
{
$tmp = explode('.',$name);
if(isset($tmp[1])){
return $this->config[$tmp[0]][$tmp[1]];
}
else{
return $this->config[$tmp[0]];
}
}