Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
很抱歉这个问题,但我只是对这个案例感到好奇。好的,例如我有一个数组元素:
<?php $data = array( 'key1' => 'val1', 'key2' => 'val2', 'key3' => $data['key2'] //the point ); ?>
我知道它会出错,因为我调用了一个元素而数组尚未声明。但有可能这样做吗?事实上,价值'key2'是动态的。
'key2'
不,但您可以轻松地做到这一点:
$data = [ "key1"=>"val1", "key2"=>"val2" ]; $data["key3"] = $data["key2"];
甚至$data["key3"] = &$data["key2"];通过引用将它们链接起来。
$data["key3"] = &$data["key2"];
PHP 按以下顺序处理:
所以不,你不能在一行中做到这一点。当然,您可以将 'val2' 分配给一个变量,然后分配给两个键。