数组可以引用自身

时间:2011-06-06 02:57:29

标签: php associative-array

我有这个关联数组,其中key2key5总是与key1具有相同的值。是否可以通过引用数组本身或任何其他建议来设置它们的值来删除值重复?

$arr = array(
   '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
);

1 个答案:

答案 0 :(得分:10)

您可以在声明数组后应用& reference

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