c Code:
PHPAPI zval *php_array_set(zval *arr, char *p1, size_t len, const zval *val)
{
zval *tmpVal;
tmpVal = zend_hash_str_find(Z_ARRVAL_P(arr), p1, len);
if (tmpVal != NULL) {
ZVAL_DEREF(tmpVal);
ZVAL_COPY(tmpVal, val);
}
return arr;
}
PHP_FUNCTION(array_set) /* {{{ */
{
zval *arr, *val;
char *key;
size_t len;
ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_ARRAY_EX(arr, 0, 1);
Z_PARAM_STRING(key, len);
Z_PARAM_ZVAL(val);
ZEND_PARSE_PARAMETERS_END();
php_array_set(arr, key, len, val);
ZVAL_COPY(return_value, arr);
}
php Code:
$arr1 = [
"a" => 1111
];
$arr2 = array_set($arr1, "a", 2222);
var_dump($arr1, $arr2);
array_set receives a reference parameter and modifies the value corresponding to the key value.
when debugging with gdb, the arr parameter is printed after calling the php_array_set method, and
but it turns out that $arr2 was modified successfully, but $arr1 is still the original value. Excuse me, what is the reason for this?