0

I have a very complex multi-dimensional array ($tree). I receive that large array as a reference.

Now, I need to find a certain key in it and insert data there.

Finding the needed key is easy. A function searches the array and returns the path $path. For instance, it returns the $path = array('index1', 'index2', 'index3'). Which means, that I would need to assign my data like $tree['index1']['index2']['index3'] = $some_data_i_needed_to_insert.

Now the problem appears is that I can't address that array index from the address I receive from the seatch function.

I tried like this:

<?php
$path = '[\'index1\'][\'index2\'][\'index3\']';
$tree{$path} = $some_data_i_needed_to_insert;
?>

Is there a way to address an array index in my case?

4
  • It's unclear what you are asking. Commented Nov 15, 2013 at 15:35
  • Could we see the first function? That might help clarify what the question is. Commented Nov 15, 2013 at 15:37
  • Why are you doing $tree{'[1334][\'#below\'][3242]'} ? Commented Nov 15, 2013 at 15:38
  • Updated the question. Sorry for being unclear. Commented Nov 15, 2013 at 15:45

1 Answer 1

2

There's no sane direct expression you can use to directly access a key if you have a path array. However, this'll do:

$path =  array('1334', '#below', '3242');
$node =& $complexArray;

foreach ($path as $key) {
    $node =& $node[$key];
}

$node = $data;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.