PHP:声明数组元素有值同样指定其他元素吗?

Please see the example:

$array = array('001' => 'A', '002' => 'B', '002' => value of 001);

I want to declare an array like above. But I need the value of element 002 is always same of element 001. How can I do that?

Please help me. Thank you so much.

Since your question does not clarify where the values are coming from, i'd just say you may use

$val1 = 'A';
$array = array(
    '001' => $val1,
    '002' => $val1,
);

To make possible that value of $array['002'] is always the same of $array['001'] you need to assign it by reference:

$array = array( '001' => 'A' );
$array['002'] = &$array['001'];
//              -

The normal assignment create a copy of original value in new variable, so — when the original variable change — the new created maintain old value. Using keyword & we can do an assignment by reference: by this way, new variable points to original variable (like an alias, or a symbolic link for files) and reflects its change.

Now, writing this:

$array['001'] = 'B';
echo $array['002'];

the result is:

B

because $array['002'] reflects $array['001'] new value.


Read more about References in php