时间:2021-07-01 10:21:17 帮助过:1人阅读
$array_a = array( 0 => 'a' );$array_b = &$array_a;$array_c = &$array_b;$array_d = $array_b;$array_c[0] = 'b';echo '变量a:';var_dump( $array_a );echo '变量b:';var_dump( $array_b );echo '变量c:';var_dump( $array_c );echo '变量d:';var_dump( $array_d );
变量a:array (size=1) 0 => string 'b' (length=1)变量b:array (size=1) 0 => string 'b' (length=1)变量c:array (size=1) 0 => string 'b' (length=1)变量d:array (size=1) 0 => string 'a' (length=1)
$array_a = array( 0 => 'a' );$array_b = &$array_a;$array_c = &$array_b[0];$array_d = $array_b;$array_c = 'b';echo '变量a:';var_dump( $array_a );echo '变量b:';var_dump( $array_b );echo '变量c:';var_dump( $array_c );echo '变量d:';var_dump( $array_d );
变量a:array (size=1) 0 => &string 'b' (length=1)变量b:array (size=1) 0 => &string 'b' (length=1)变量c:string 'b' (length=1)变量d:array (size=1) 0 => &string 'b' (length=1)
手册里说了:如果具有引用的数组被拷贝,其值不会解除引用。对于数组传值给函数也是如此。
$array_c = &$array_b[0]; // 这样使得$array_b具有了引用,c与b[0]指向同一内存
上例子: