0

I have an array like this:

array(
      0 => array(
                 0 => array(
                            0 => array()
                 ),
                 1 => array(
                            1 => array()
                 )
      ),
      1 => array(
                 0 => array(
                            0 => array()
                 ),
                 1 => array(
                            1 => array()
                 )
      )
 )

There may be a syntax error in that array but it's not important

Then I have second array: $pathArray = array(0 => 1, 1 => 0, 2 => 1) which indicates a path (1,0,1) in the first array where my variable $var = "test"should be placed. So in the end it would look like:

array(
      0 => array(
                 0 => array(
                            0 => array()
                 ),
                 1 => array(
                            1 => array()
                 )
      ),
      1 => array(
                 0 => array(
                            0 => array()
                 ),
                 1 => array(
                            1 => array("test")
                 )
      )
 )

How can I do that? Hope it's clear. Thanks

1 Answer 1

1
$c = &$array;
for($i=0; $i<count($pathArray); $i++){
    $c = &$c[$pathArray[$i]]; 
}
$c[] = "test";
Sign up to request clarification or add additional context in comments.

2 Comments

seems cool. Just one question: what does those [] stands for exactly in general?
[] means to add an element to the end of the array

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.