cvs: php4 /ext/standard array.c basic_functions.c php_array.h

From: Date: Sat, 27 Oct 2001 05:11:15 +0000
Subject: cvs: php4 /ext/standard array.c basic_functions.c php_array.h
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-7986@lists.php.net to get a copy of this message
andrei Sat Oct 27 01:11:15 2001 EDT Modified files: /php4/ext/standard array.c basic_functions.c php_array.h Log: @- Added array_chunk() function that splits an array into chunks of specified @ size. (Andrei) Index: php4/ext/standard/array.c diff -u php4/ext/standard/array.c:1.145 php4/ext/standard/array.c:1.146 --- php4/ext/standard/array.c:1.145 Tue Oct 23 14:46:35 2001 +++ php4/ext/standard/array.c Sat Oct 27 01:11:14 2001 @@ -21,7 +21,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: array.c,v 1.145 2001/10/23 18:46:35 rasmus Exp $ */ +/* $Id: array.c,v 1.146 2001/10/27 05:11:14 andrei Exp $ */ #include "php.h" #include "php_ini.h" @@ -3273,6 +3273,77 @@ RETURN_FALSE; } +} +/* }}} */ + + +/* {{{ proto array array_chunk(array input, int size [, bool preserve_keys]) + Split array into chunks */ +PHP_FUNCTION(array_chunk) +{ + int argc = ZEND_NUM_ARGS(), key_type; + long size, current = 0; + char *str_key; + uint str_key_len; + ulong num_key; + zend_bool preserve_keys = 0; + zval *input = NULL; + zval *chunk = NULL; + zval **entry; + HashPosition pos; + + if (zend_parse_parameters(argc TSRMLS_CC, "al|b", &input, &size, + &preserve_keys) == FAILURE) + return; + + /* Do bounds checking for size parameter. */ + if (size < 1) { + php_error(E_WARNING, "%s() expects the size parameter to be > 0", + get_active_function_name(TSRMLS_C)); + return; + } + + array_init(return_value); + + zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(input), &pos); + while (zend_hash_get_current_data_ex(Z_ARRVAL_P(input), (void**)&entry, &pos) == SUCCESS) { + /* If new chunk, create and initialize it. */ + if (!chunk) { + MAKE_STD_ZVAL(chunk); + array_init(chunk); + } + + /* Add entry to the chunk, preserving keys if necessary. */ + zval_add_ref(entry); + + if (preserve_keys) { + key_type = zend_hash_get_current_key_ex(Z_ARRVAL_P(input), &str_key, + &str_key_len, &num_key, 0, &pos); + if (key_type == HASH_KEY_IS_STRING) { + add_assoc_zval_ex(chunk, str_key, str_key_len, *entry); + } else { + add_index_zval(chunk, num_key, *entry); + } + } else { + add_next_index_zval(chunk, *entry); + } + + /* + * If reached the chunk size, add it to the result array, and reset the + * pointer. + */ + if (!(++current % size)) { + add_next_index_zval(return_value, chunk); + chunk = NULL; + } + + zend_hash_move_forward_ex(Z_ARRVAL_P(input), &pos); + } + + /* Add the final chunk if there is one. */ + if (chunk) { + add_next_index_zval(return_value, chunk); + } } /* }}} */ Index: php4/ext/standard/basic_functions.c diff -u php4/ext/standard/basic_functions.c:1.412 php4/ext/standard/basic_functions.c:1.413 --- php4/ext/standard/basic_functions.c:1.412 Tue Oct 23 14:46:35 2001 +++ php4/ext/standard/basic_functions.c Sat Oct 27 01:11:15 2001 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: basic_functions.c,v 1.412 2001/10/23 18:46:35 rasmus Exp $ */ +/* $Id: basic_functions.c,v 1.413 2001/10/27 05:11:15 andrei Exp $ */ #include "php.h" #include "php_main.h" @@ -781,6 +781,7 @@ PHP_FE(array_sum, NULL) PHP_FE(array_filter, NULL) PHP_FE(array_map, NULL) + PHP_FE(array_chunk, NULL) PHP_FE(key_exists, NULL) /* aliases from array.c */ Index: php4/ext/standard/php_array.h diff -u php4/ext/standard/php_array.h:1.31 php4/ext/standard/php_array.h:1.32 --- php4/ext/standard/php_array.h:1.31 Tue Oct 23 14:46:35 2001 +++ php4/ext/standard/php_array.h Sat Oct 27 01:11:15 2001 @@ -19,7 +19,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_array.h,v 1.31 2001/10/23 18:46:35 rasmus Exp $ */ +/* $Id: php_array.h,v 1.32 2001/10/27 05:11:15 andrei Exp $ */ #ifndef PHP_ARRAY_H #define PHP_ARRAY_H @@ -80,6 +80,7 @@ PHP_FUNCTION(array_filter); PHP_FUNCTION(array_map); PHP_FUNCTION(key_exists); +PHP_FUNCTION(array_chunk); HashTable* php_splice(HashTable *, int, int, zval ***, int, HashTable **); PHPAPI void php_array_merge(HashTable *dest, HashTable *src, int recursive);

« previous php.cvs (#7986) next »