cvs: php4 /ext/standard array.c basic_functions.c basic_functions.h
| From: | Derick Rethans | Date: | Sat, 29 Dec 2001 20:59:59 +0000 |
| Subject: | cvs: php4 /ext/standard array.c basic_functions.c basic_functions.h | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-8847@lists.php.net to get a copy of this message | ||
derick Sat Dec 29 15:59:59 2001 EDT
Modified files:
/php4/ext/standard array.c basic_functions.c basic_functions.h
Log:
- Added extra parameter to count() that recursively counts elements in an
array and added is_array_multidimensional(). (patch by Vlad Bosinceanu
<glipy@fx.ro>)
Index: php4/ext/standard/array.c
diff -u php4/ext/standard/array.c:1.150 php4/ext/standard/array.c:1.151
--- php4/ext/standard/array.c:1.150 Sat Dec 29 15:47:25 2001
+++ php4/ext/standard/array.c Sat Dec 29 15:59:59 2001
@@ -21,11 +21,10 @@
+----------------------------------------------------------------------+
*/
-/* $Id: array.c,v 1.150 2001/12/29 20:47:25 andi Exp $ */
+/* $Id: array.c,v 1.151 2001/12/29 20:59:59 derick Exp $ */
#include "php.h"
#include "php_ini.h"
-#include "zend_operators.h"
#include <stdarg.h>
#include <stdlib.h>
#include <math.h>
@@ -68,6 +67,8 @@
#define CASE_LOWER 0
#define CASE_UPPER 1
+#define COUNT_NORMAL 0
+#define COUNT_RECURSIVE 1
PHP_MINIT_FUNCTION(array)
{
@@ -90,6 +91,9 @@
REGISTER_LONG_CONSTANT("CASE_LOWER", CASE_LOWER, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("CASE_UPPER", CASE_UPPER, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("COUNT_NORMAL", COUNT_NORMAL, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("COUNT_RECURSIVE", COUNT_RECURSIVE, CONST_CS | CONST_PERSISTENT);
+
return SUCCESS;
}
@@ -223,26 +227,45 @@
}
/* }}} */
-/* {{{ proto int count(mixed var)
- Count the number of elements in a variable (usually an array) */
-PHP_FUNCTION(count)
+
+int php_count_recursive(zval *array, long mode)
{
- pval **array;
+ long cnt = 0, i;
+ zval **element;
+
HashTable *target_hash;
+ target_hash = HASH_OF(array);
- if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &array) == FAILURE) {
- WRONG_PARAM_COUNT;
- }
- target_hash = HASH_OF(*array);
- if (!target_hash) {
- if (Z_TYPE_PP(array) == IS_NULL) {
- RETURN_LONG(0);
- } else {
- RETURN_LONG(1);
+ if (Z_TYPE_P(array) == IS_ARRAY)
+ {
+ cnt += zend_hash_num_elements(target_hash);
+ if (mode == COUNT_RECURSIVE) {
+ for(i = 0; i < zend_hash_num_elements(target_hash); i++) {
+ if (zend_hash_index_find (Z_ARRVAL_P(array), i, (void **) &element) == SUCCESS) {
+ cnt += php_count_recursive(*element, COUNT_RECURSIVE);
+ }
+ }
}
}
+ return cnt;
+}
- RETURN_LONG(zend_hash_num_elements(target_hash));
+/* {{{ proto int count(mixed var [, int mode])
+ Count the number of elements in a variable (usually an array) */
+PHP_FUNCTION(count)
+{
+ zval *array;
+ long mode = COUNT_NORMAL;
+
+ if (zend_parse_parameters (ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &array, &mode) ==
FAILURE)
+ return;
+
+ if (Z_TYPE_P(array) == IS_ARRAY) {
+ RETURN_LONG (php_count_recursive (array, mode));
+ } else {
+ /* return 1 for non-array arguments */
+ RETURN_LONG(1);
+ }
}
/* }}} */
Index: php4/ext/standard/basic_functions.c
diff -u php4/ext/standard/basic_functions.c:1.430 php4/ext/standard/basic_functions.c:1.431
--- php4/ext/standard/basic_functions.c:1.430 Tue Dec 11 10:30:28 2001
+++ php4/ext/standard/basic_functions.c Sat Dec 29 15:59:59 2001
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: basic_functions.c,v 1.430 2001/12/11 15:30:28 sebastian Exp $ */
+/* $Id: basic_functions.c,v 1.431 2001/12/29 20:59:59 derick Exp $ */
#include "php.h"
#include "php_main.h"
@@ -520,6 +520,7 @@
PHP_FE(is_numeric, NULL)
PHP_FE(is_string, NULL)
PHP_FE(is_array, NULL)
+ PHP_FE(is_array_multidimensional, NULL)
PHP_FE(is_object, NULL)
PHP_FE(is_scalar, NULL)
PHP_FE(is_callable, third_arg_force_ref)
@@ -1630,6 +1631,31 @@
PHP_FUNCTION(is_array)
{
php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_ARRAY);
+}
+/* }}} */
+
+/* {{{ proto bool is_array_multidimensional(mixed var)
+ Returns true if variable is a multidimensional array */
+PHP_FUNCTION(is_array_multidimensional)
+{
+ zval *var, **element;
+ int i = 0;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &var) == FAILURE) {
+ return;
+ }
+
+ if (Z_TYPE_P(var) == IS_ARRAY) {
+ while (zend_hash_num_elements (HASH_OF(var)) > i) {
+ if (zend_hash_index_find (Z_ARRVAL_P(var), i, (void **) &element) == SUCCESS) {
+ if(Z_TYPE_PP(element) == IS_ARRAY) {
+ RETURN_TRUE;
+ }
+ }
+ i++;
+ }
+ RETURN_FALSE;
+ }
}
/* }}} */
Index: php4/ext/standard/basic_functions.h
diff -u php4/ext/standard/basic_functions.h:1.99 php4/ext/standard/basic_functions.h:1.100
--- php4/ext/standard/basic_functions.h:1.99 Tue Dec 11 10:30:28 2001
+++ php4/ext/standard/basic_functions.h Sat Dec 29 15:59:59 2001
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: basic_functions.h,v 1.99 2001/12/11 15:30:28 sebastian Exp $ */
+/* $Id: basic_functions.h,v 1.100 2001/12/29 20:59:59 derick Exp $ */
#ifndef BASIC_FUNCTIONS_H
#define BASIC_FUNCTIONS_H
@@ -72,6 +72,7 @@
PHP_FUNCTION(is_numeric);
PHP_FUNCTION(is_string);
PHP_FUNCTION(is_array);
+PHP_FUNCTION(is_array_multidimensional);
PHP_FUNCTION(is_object);
PHP_FUNCTION(is_scalar);
PHP_FUNCTION(is_callable);