- 论坛徽章:
- 0
|
本帖最后由 无野 于 2010-06-15 16:29 编辑
array_combine是PHP5一个standard的array函数。
它的作用是创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值。
array array_combine ( array $keys, array $values )
返回一个 array,用来自 keys 数组的值作为键名,来自 values 数组的值作为相应的值。
如果两个数组的单元数不同或者数组为空时返回 FALSE。
注意粗体字的描述,也就是说,如果你使用array_combine,在没有做好兼容性的情况下,便有因为两数组单元素不同而导致程序异常。
首先不讨论这种异常对于程序来说是好处还是坏处,我认为至少函数应该要给程序员一个可选的严格模式。
我决定对array_combine做这个修改,即添加一个boolean参数use_strict,默认值为true。这样可以兼容原生的函数。
vi php-5.3.1/ext/standard/array.c, 将array_combine函数原型源码修改成如下:- /* {{{ proto array array_combine(array keys, array values)
- Creates an array by using the elements of the first parameter as keys and the elements of the second as the corresponding values
- */
- PHP_FUNCTION(array_combine)
- {
- zval *values, *keys;
- HashPosition pos_values, pos_keys;
- zval **entry_keys, **entry_values;
- int num_keys, num_values;
- zend_bool use_strict = 1;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "aa|b", &keys, &values, &use_strict) == FAILURE) {
- return;
- }
- num_keys = zend_hash_num_elements(Z_ARRVAL_P(keys));
- num_values = zend_hash_num_elements(Z_ARRVAL_P(values));
- if (use_strict){
- if (num_keys != num_values) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Both parameters should have an equal number of elements");
- RETURN_FALSE;
- }
- }
- if (!num_keys) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Both parameters should have at least 1 element");
- RETURN_FALSE;
- }
- array_init_size(return_value, num_keys);
- zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(keys), &pos_keys);
- zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(values), &pos_values);
- while (zend_hash_get_current_data_ex(Z_ARRVAL_P(keys), (void **)&entry_keys, &pos_keys) == SUCCESS &&
- zend_hash_get_current_data_ex(Z_ARRVAL_P(values), (void **)&entry_values, &pos_values) == SUCCESS
- ) {
- if (Z_TYPE_PP(entry_keys) == IS_LONG) {
- zval_add_ref(entry_values);
- add_index_zval(return_value, Z_LVAL_PP(entry_keys), *entry_values);
- } else {
- zval key, *key_ptr = *entry_keys;
- if (Z_TYPE_PP(entry_keys) != IS_STRING) {
- key = **entry_keys;
- zval_copy_ctor(&key);
- convert_to_string(&key);
- key_ptr = &key;
- }
- zval_add_ref(entry_values);
- add_assoc_zval_ex(return_value, Z_STRVAL_P(key_ptr), Z_STRLEN_P(key_ptr) + 1, *entry_values);
- if (key_ptr != *entry_keys) {
- zval_dtor(&key);
- }
- }
- zend_hash_move_forward_ex(Z_ARRVAL_P(keys), &pos_keys);
- zend_hash_move_forward_ex(Z_ARRVAL_P(values), &pos_values);
- }
- }
- /* }}} */
复制代码 这样,array_combine就变成了:
array array_combine ( array $keys, array $values [, bool $use_strict] )
因为我在一台测试上做这个事,所以我重编译了PHP。
我们来测一下修改的效果。
使用默认的严格模式:- $a = array('green', 'red');
- $b = array('avocado', 'apple', 'banana');
- $c = array_combine($a, $b);
- print_r($c);
复制代码- [root@tekeq test]# /wwws/php/bin/php test.php
- PHP Warning: array_combine(): Both parameters should have an equal number of elements in /root/test/test.php on line 5
复制代码 使用非严格模式:- $a = array('green', 'red');
- $b = array('avocado', 'apple', 'banana');
- $c = array_combine($a, $b, false); //使用非严格模式
- print_r($c);
复制代码- [root@tekeq test]# /wwws/php/bin/php test.php
- Array
- (
- [green] => avocado
- [red] => apple
- )
复制代码 看起来一切正常,就这么办。
认为有需要的同学,可以改一下,这修改后的是兼容以前的。
如果觉得没必要就算了,我是有需要才改,不是改着玩。
相关文章:http://tekeq.com/tech/array_combine/ |
|