21.8 Looping Through Arrays


Your function loops through an array and you want to perform an action on, or do something with, every element in that array.

Technique

For numerically indexed arrays, use the zend_hash_index_find() function along with the zend_hash_num_elements() function:

 PHP_FUNCTION(print_array) {     zval **ar, **var = NULL;     HashTable *array;     char *string_key = NULL;     ulong num_key;     if (ZEND_NUM_ARGS() != 1          zend_get_parameters_ex(1, &ar) == FAILURE) {         WRONG_PARAM_COUNT;     }     array = HASH_OF(*ar);     for (zend_hash_internal_pointer_reset(array);          zend_hash_get_current_data(array, (void **)&var) == SUCCESS;          zend_hash_move_forward(array)) {         SEPARATE_ZVAL(var);         convert_to_string_ex(var);         switch (zend_hash_get_current_key(array, &string_key, &num_key)) {             case HASH_KEY_IS_LONG:                 php_printf("Element %d: %s", num_key, Z_STRVAL_PP(var));                 break;             case HASH_KEY_IS_STRING:                 php_printf("Element %s: %s", string_key, Z_STRVAL_PP(var));                 efree(string_key);                 break;         }     } } 

Comments

Here we use a combination of the zend_hash_internal_pointer_reset() , zend_hash_get_current_data() , and zend_hash_move_forward() functions to loop through the array. We then use the zend_hash_get_current_key() function to fetch the key relating to the current value being manipulated. If the key is a string, the value of the key is placed in the string_key variable; otherwise , the contents are placed in the num_key variable.

The preceding method, although simple, affects the current position of the array pointer, meaning that after we've looped through the array once, the zend_hash_internal_pointer_reset() function needs to be called on it for the array to be processed again. If you do not want to affect the internal array pointer, use the *_ex() counterparts to these functions:

 PHP_FUNCTION(print_array) {     zval **ar, **var = NULL;     HashTable *array;  HashPosition pos;  char *string_key = NULL;     ulong num_key;     if (ZEND_NUM_ARGS() != 1          zend_get_parameters_ex(1, &ar) == FAILURE) {         WRONG_PARAM_COUNT;     }     array = HASH_OF(*ar);     for (zend_hash_internal_pointer_reset_ex(array,  &pos  );          zend_hash_get_current_data_ex(array, (void **)&var,  &pos  ) == SUCCESS;          zend_hash_move_forward_ex(array,  &pos  )) {         SEPARATE_ZVAL(var);         convert_to_string_ex(var);        switch (zend_hash_get_current_key(array, &string_key, &num_key,  &pos  )) {             case HASH_KEY_IS_LONG:                 php_printf("Element %d: %s", num_key, Z_STRVAL_PP(var));                 break;             case HASH_KEY_IS_STRING:                 php_printf("Element %s: %s", string_key, Z_STRVAL_PP(var));                 efree(string_key);                 break;         }     } } 


PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net