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 ?

4

2 回答 2

2

你可以这样做:

$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
于 2013-09-09T11:56:00.307 回答
0

当您编写 $variable = array[key][key]... 时,您将数组的值传递给变量。如果要更改数组本身的值,则需要这样做:

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

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

print_r($myArray);
于 2013-09-09T11:55:44.147 回答