有没有办法从数组中引用数组键?这在代码格式中可能更有意义:
$array=array(
"Key1"=>array(
"Value1",
"Value2"
),
"Key2"=>&$this['Key1']
);
我想要的是为了$array['Key2']
输出与$array['Key1']
. I can add $array['Key2']=&$array['Key1'];
创建数组后,但如果可能的话希望将其全部保留在一个代码块中。
我检查了有关参考的文档,以及一些建议的类似问题,并搜索“php 数组参考”。
事实证明,答案是肯定的。然而,它不是一个整洁的语法,因为它使用了一种子语句,并且使当前范围散布着额外的引用变量。
考虑以下代码:
<?php
$array = array(
// Creates Key1 and assigns the value to it
// A copy of the value is also placed in $ref
// At this stage, it's not a reference
"Key1"=>($ref = array(
"Value1",
"Value2"
)),
// Now Key2 is a reference to $ref, but not to Key1
"Key2"=>&$ref,
// Now everything is referenced together
"Key1"=>&$ref
);
我很惊讶这没有错误,但它确实 -这是证据 http://codepad.org/BAUeXMJ3。当然,你wouldn't这样做,但你可以...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)