开发者

Can an array refer to itself

开发者 https://www.devze.com 2023-03-11 02:42 出处:网络
I have this associative array, where key2 and key5 are always going to have the same value as key1. Is it possible to set their values by referring to the array itself or any other suggestions to remo

I have this associative array, where key2 and key5 are always going to have the same value as key1. Is it possible to set their values by referring to the array itself or any other suggestions to remove the value duplication?

$arr = array(
 开发者_StackOverflow中文版  'key1' => 'some value',
   'key2' => 'some value', //same as key1 and will always stay as key1
   'key3' => 'some other value',
   'key4' => 'yet another',
   'key5' => 'some value'  //same as key1 and will always stay as key1
);


You could apply a & reference after the array has been declared:

$arr = array(...);
$arr["key2"] = & $arr["key1"];
$arr["key5"] = & $arr["key1"];
0

精彩评论

暂无评论...
验证码 换一张
取 消