php - Dynamic multidimensional array keys for class property


Is it possible to create multidimensional array with its keys defined in array? Yes, it is, according to bunch of Stack Overflow answers. Here is one: Dynamic array keys

function insert_using_keys($arr, array $path, $value) { // See linked answer }
$arr = create_multi_array($arr, array('a', 'b', 'c'), 'yay'));


Array ( [a] => Array ( [b] => Array ( [c] => yay ) ) ) 

Would the same be possible for class properties?

This is a barebone version of my Collection class. Methodset_at should add a multidimensional array to$data property the same wayinsert_using_keys function does.

class A {

    protected $data = array();

    public function set($key, $value) {
         $this->data[$key] = $value;

    public function set_at(array $keys, $value) {



I've tried a several modifications of theinsert_using_keys to no avail. I was able to set the keys to the property, but not assign value "to the last one".

Would someone point me in the right direction please? Thanks in advance!




In the midst of recreating the "key-setter" function, I was able to answer my own question (was it your intention all along, Stefan?).

Here is the code:

public function set_at(array $keys, $value) {

    $first_key = array_shift($keys);

    foreach (array_reverse($keys) as $key) {
        $value = array($key => $value);

    $this->data[$first_key] = $value;


People are also looking for solutions to the problem: php - Run a Zend1 app on localhost


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.