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 ?

Sumit Bijvani
8,17518 gold badges52 silver badges83 bronze badges
asked Sep 9, 2013 at 11:52

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
answered Sep 9, 2013 at 11:56
0
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);
answered Sep 9, 2013 at 11:55
2
  • 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. Commented Sep 9, 2013 at 11:57
  • In that case @Akam answer is the way to go. Commented Sep 9, 2013 at 12:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.