Hi,
I have written a patch for the above feature request to implement an
array_swap function.
array_swap($array, $key1, $key2), where keys can be either index or
assoc and array content can be any combination of string, long, array
and object.
This is my first patch and I am hoping to contribute more to php in the
future so if you don't want to use the patch for any reason, I would be
grateful for any pointers or comments on the code.
Regards
Tony Leake
diff -Nur php-4.2.2/ext/standard/array.c php-4.2.2_devel/ext/standard/array.c
--- php-4.2.2/ext/standard/array.c 2002-04-24 16:29:38.000000000 +0100
+++ php-4.2.2_devel/ext/standard/array.c 2002-08-27 19:09:16.000000000 +0100
@@ -3366,6 +3366,89 @@
}
/* }}} */
+
+/* {{{ proto bool array_swap(array input_array, int index1, int index2)
+ swaps 2 elements in an array */
+PHP_FUNCTION( array_swap )
+{
+ zval *input_arr, *temp_arr;
+ zval **tmp1, **tmp2;
+ zval *key1;
+ zval *key2;
+
+ if ( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "azz", &input_arr, &key1, &key2 ) !=SUCCESS ){
+ RETURN_FALSE;
+ }
+
+
+ MAKE_STD_ZVAL( temp_arr );
+ *temp_arr = *input_arr;
+ SEPARATE_ZVAL( &temp_arr );
+
+ if ( Z_TYPE_P(key1)==IS_LONG && Z_TYPE_P(key2)==IS_LONG ){
+
+ if ( zend_hash_index_find( Z_ARRVAL_P( temp_arr ), Z_LVAL_P( key1 ), (void**)&tmp1 )!=SUCCESS){
+ php_error( E_WARNING, "Key 1 does not exist" );
+ return;
+ }
+
+ if ( zend_hash_index_find( Z_ARRVAL_P( temp_arr ), Z_LVAL_P( key2 ), (void**)&tmp2 )!=SUCCESS){
+ php_error( E_WARNING, "Key 2 does not exist" );
+ return;
+ }
+
+ add_index_zval( input_arr, Z_LVAL_P( key1 ), *tmp2 );
+ add_index_zval( input_arr, Z_LVAL_P( key2 ), *tmp1 );
+
+ } else if ( Z_TYPE_P( key1 )==IS_LONG && Z_TYPE_P( key2 )==IS_STRING ){
+
+ if ( zend_hash_index_find( Z_ARRVAL_P( temp_arr ), Z_LVAL_P( key1 ), (void**)&tmp1 )!=SUCCESS){
+ php_error( E_WARNING, "Key 1 does not exist" );
+ return;
+ }
+
+ if ( zend_hash_find( Z_ARRVAL_P( temp_arr ), Z_STRVAL_P( key2 ), Z_STRLEN_P( key2 )+1, (void**)&tmp2 )!=SUCCESS){
+ php_error( E_WARNING, "Key 2 does not exist" );
+ return;
+ }
+
+ add_index_zval( input_arr, Z_LVAL_P( key1 ), *tmp2 );
+ add_assoc_zval( input_arr, Z_STRVAL_P( key2 ), *tmp1);
+
+ } else if ( Z_TYPE_P( key1 )==IS_STRING && Z_TYPE_P( key2 )==IS_LONG){
+
+ if ( zend_hash_find( Z_ARRVAL_P( temp_arr ), Z_STRVAL_P(key1), Z_STRLEN_P( key1 )+1, (void**)&tmp1 )!=SUCCESS){
+ php_error( E_WARNING, "Key 1 does not exist" );
+ return;
+ }
+
+ if ( zend_hash_index_find( Z_ARRVAL_P( temp_arr ), Z_LVAL_P( key2 ), (void**)&tmp2 )!=SUCCESS){
+ php_error( E_WARNING, "Key 2 does not exist" );
+ return;
+ }
+
+ add_assoc_zval( input_arr, Z_STRVAL_P( key1 ), *tmp2);
+ add_index_zval( input_arr, Z_LVAL_P( key2 ), *tmp1 );
+
+ }else{
+
+ if ( zend_hash_find( Z_ARRVAL_P( temp_arr ), Z_STRVAL_P( key1 ), Z_STRLEN_P( key1 )+1, (void**)&tmp1 )!=SUCCESS){
+ php_error( E_WARNING, "Key 1 does not exist" );
+ return;
+ }
+
+ if ( zend_hash_find( Z_ARRVAL_P( temp_arr ), Z_STRVAL_P(key2), Z_STRLEN_P( key2 )+1, (void**)&tmp2 )!=SUCCESS){
+ php_error( E_WARNING, "Key 2 does not exist" );
+ return;
+ }
+
+ add_assoc_zval( input_arr, Z_STRVAL_P( key1 ), *tmp2);
+ add_assoc_zval( input_arr, Z_STRVAL_P( key2 ), *tmp1);
+ }
+
+ RETURN_TRUE;
+}
+/* }}} */
/*
* Local variables:
* tab-width: 4
diff -Nur php-4.2.2/ext/standard/basic_functions.c php-4.2.2_devel/ext/standard/basic_functions.c
--- php-4.2.2/ext/standard/basic_functions.c 2002-05-11 20:23:05.000000000 +0100
+++ php-4.2.2_devel/ext/standard/basic_functions.c 2002-08-27 19:11:34.000000000 +0100
@@ -793,6 +793,7 @@
PHP_FE(array_map, NULL)
PHP_FE(array_chunk, NULL)
PHP_FE(array_key_exists, NULL)
+ PHP_FE(array_swap, first_arg_force_ref)
/* aliases from array.c */
PHP_FALIAS(pos, current, first_arg_force_ref)
diff -Nur php-4.2.2/ext/standard/php_array.h php-4.2.2_devel/ext/standard/php_array.h
--- php-4.2.2/ext/standard/php_array.h 2001-12-11 15:30:34.000000000 +0000
+++ php-4.2.2_devel/ext/standard/php_array.h 2002-08-27 19:09:39.000000000 +0100
@@ -81,6 +81,7 @@
PHP_FUNCTION(array_map);
PHP_FUNCTION(array_key_exists);
PHP_FUNCTION(array_chunk);
+PHP_FUNCTION(array_swap);
HashTable* php_splice(HashTable *, int, int, zval ***, int, HashTable **);
PHPAPI void php_array_merge(HashTable *dest, HashTable *src, int recursive);
--
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php