php内置函数分析之array_fill_keys()
1 PHP_FUNCTION(array_fill_keys) 2 { 3 zval *keys, *val, *entry; 4 5 if (zend_parse_parameters(ZEND_NUM_ARGS(), "az", &keys, &val) == FAILURE) { 6 return; 7 } 8 9 /* Initialize return array */ 10 array_init_size(return_value, zend_hash_num_elements(Z_ARRVAL_P(keys))); 11 12 // 遍历数组keys 13 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(keys), entry) { 14 ZVAL_DEREF(entry); 15 Z_TRY_ADDREF_P(val); 16 /* 以数组key值为键,val为值。插入返回的数组return_value */ 17 if (Z_TYPE_P(entry) == IS_LONG) { 18 zend_hash_index_update(Z_ARRVAL_P(return_value), Z_LVAL_P(entry), val); 19 } else { 20 zend_string *key = zval_get_string(entry); 21 zend_symtable_update(Z_ARRVAL_P(return_value), key, val); 22 zend_string_release(key); 23 } 24 } ZEND_HASH_FOREACH_END(); 25 }