我有这个关联数组,其中key2
和key5
总是与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
);
答案 0 :(得分:10)
您可以在声明数组后应用&
reference:
$arr = array(...);
$arr["key2"] = & $arr["key1"];
$arr["key5"] = & $arr["key1"];