0

I want to do something like this:

$myArray = array(
'1' => array('2' => array('3' => 'Test'))
);

$changeIt = $myArray['1']['2']['3'];
$changeIt = 'Changed Test';

// $myArray['1']['2']['3'] is now "Changed Test"

Are there ways to do this ?

2 Answers 2

2

You can do like this:

$myArray = array(
'1' => array('2' => array('3' => 'Test'))
);

$myArray['1']['2']['3'] = &$changeIt;//reference
$changeIt = 'Changed Test'; 
echo  $myArray['1']['2']['3']; //Changed Test

$changeIt = 'Another test';
echo $myArray['1']['2']['3']; //Another test
Sign up to request clarification or add additional context in comments.

Comments

0

When you write $variable = array[key][key]... you are passing value of the array to the variable. If you want to change value of array itself you need to do it like this:

$myArray = array(
'1' => array('2' => array('3' => 'Test'))
);

$myArray['1']['2']['3'] = 'Changed Test';

print_r($myArray);

2 Comments

I know that i can do it this way - but in my problem i have to solve it is not possible to call the array like this and i have to create a reference to change it.
In that case @Akam answer is the way to go.

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.