I have a nested array, and I have a reference to an element within that array. I would like to be able to assign a value inside that array using my reference, but I can't see how to do that.
$arr = ['foo' => ['bar' => 'Hello World']];
$ref =& $arr['foo']['bar'];
var_export($arr);
// array (
// 'foo' =>
// array (
// 'bar' => 'Hello World',
// ),
// )
var_export($ref);
// 'Hello World'
$arr['foo']['bar'] = 'Test';
var_export($ref);
// 'Test'
The above works fine, but I now want to assign a new value to 'bar' using my reference:
&$ref = 'fooz';
PHP Parse error: syntax error, unexpected '&', expecting end of file in php shell code on line 1
I tried this, but it's a nope:
$ref =& 'fooz';
PHP Parse error: syntax error, unexpected ';', expecting :: (T_PAAMAYIM_NEKUDOTAYIM) in php shell code on line 1
And slightly worryingly, attempting this assignment appears to corrupt data:
$ref &= 'fooz';
var_export($ref);
// '@elh'
var_export($arr);
// array (
// 'foo' =>
// array (
// 'bar' => '@elh',
// ),
// )
Background: I have an array that may or may not be nested, and thus it's kind of hard to find the leaf-node where I want to assign things. So I had hoped to get a reference to said leaf-node, and then everything would be lovely.
I have PHP running on Linux Mint 20.2 (64bit).
$ php --version
PHP 7.4.3 (cli) (built: Jul 5 2021 15:13:35) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
Additional 20210907-1518: I am using the following function to set my reference:
function &getNestedArrayRef($array, array $keys) {
$key = array_shift($keys);
if (!is_null($key)) {
return getNestedArrayRef($array[$key], $keys);
}
return $array;
}
$ref = &getNestedArrayRef($arr, ['foo', 'bar]);
// The above _should_ be equivalent to:
$ref =& $arr['foo']['bar'];
^ it's entirely possible this isn't working as intended!
However, without it, it's kind of tricky to get a reference to an arbitrary point within the array without a literal reference.
Think you are making life too complex.
Using the=&
makes$ref
a reference, so you dont need to use the&
reference idea on it again.
$arr = ['foo' => ['bar' => 'Hello World']];
$ref =& $arr['foo']['bar'];
$ref = 'Hi Boyo';
var_dump($arr);
RESULT
array(1) {
'foo' => array(1) {
'bar' => string(7) "Hi Boyo"
}
}
Second try, when real code was involved
$arr = ['foo' => ['bar' => 'Hello World']];
function &getNestedArrayRef(&$array, array $keys) {
$key = array_shift($keys);
if (!is_null($key)) {
return getNestedArrayRef($array[$key], $keys);
}
return $array;
}
$ref =& getNestedArrayRef($arr, ['foo', 'bar']);
$ref = 'Bye';
print_r($arr);
RESULT
Array
(
[foo] => Array
(
[bar] => Bye
)
)
You also need to pass the array to the function by reference
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.