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
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
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
-
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.TJR– TJR2013年09月09日 11:57:34 +00:00Commented Sep 9, 2013 at 11:57
-
In that case @Akam answer is the way to go.Dexa– Dexa2013年09月09日 12:02:37 +00:00Commented Sep 9, 2013 at 12:02
lang-php