I have built a function to retrieve the last specified key of an array, it references the array and displays it fine, however, when I try to add to the reference variable, it does not affect the referenced array.
Here is my code:
class test {
public function __construct() {
// For testing
$this->keys[] = 'key1';
$this->keys[] = 'key2';
$this->array['key1']['key2'] = 'Hello World';
}
protected function &getArray() {
$array = &$this->array;
foreach($this->keys as $key) {
$array = &$array[$key];
}
return $array;
}
function add() {
$tmpArray = $this->getArray();
print_r($tmpArray);
echo "<br>\n";
$tmpArray = 'Goodbye World';
print_r($tmpArray);
echo "<br>\n";
print_r($this->array);
}
}
$test = new test;
$test->add();
To sum it up, add() and __construct() are for testing. I am trying to add to $this->array with add(). However when I specify $tmpArray = 'Goodbye World' the referenced array $this->array['key1']['key2'] is still Hello World.
Could someone help point me in the right direction?
$this->arraysince key2 is the last specified in$this->keys. $tmpArray is equal to Hello World from the reference before I set it to Goodbye World. But I am trying to get$this->array['key1']['key2']to equal Goodbye World too from the reference.$tagsexist in the class variables, where does$valuesexist in the class variables, you must be getting errors? I can't see any variable declarations insidetest.