Extending PHP - working with hash tables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
musashi
Forum Commoner
Posts: 39
Joined: Tue Jul 23, 2002 12:51 pm
Location: Santa Cruz - CA

Extending PHP - working with hash tables

Post by musashi »

To let everyone know, I am referring to building a C extension that compiles into the PHP core. The extensions have 2 functions that seem to be not happy working with each other.

So for this to not be to confusing, I have:

A) [add_to_array] function that feeds in the contents of an array and then returns the array with an additional element.

So far so good. This works fine. Then I have:

B) [get_array_info] Function that takes in an array, iterates through it, builds another array and returns that.

Here is the problem. In PHP the activity looks like this:

Code: Select all

$stuff_to_add = array("test"=>"info","test2"=>"info2");
$data = add_to_array($stuff_to_add,$data);

print_r(get_array_info($data));
Now the C extension functions look like this (reduced for display purposes. If you need more of it, let me know):

Code: Select all

PHP_FUNCTION(add_to_array)
{
//omitted

if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "aa",&arr,&arr2) == FAILURE)
		RETURN_FALSE;

//omitted

while(zend_hash_get_current_data(arr->value.ht, (void **)&data) == SUCCESS) {

// get data from stuff_to_add

  zend_hash_move_forward(arr->value.ht);
	}

while(zend_hash_get_current_data(arr2->value.ht, (void **)&data) == SUCCESS) {
// do the additions to the array here
		
  zend_hash_move_forward(arr2->value.ht);
	}

//omitted stuff

	*return_value = *arr2;
	zval_copy_ctor(return_value);
}


PHP_FUNCTION(get_array_info)
{
//omitted

	if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a",&arr) == FAILURE)
		RETURN_FALSE;

//omitted stuff

zend_hash_internal_pointer_reset(arr->value.ht);
while(zend_hash_get_current_data(arr->value.ht, (void **)&data) == SUCCESS) {

//iterate through arr and build new array (return_value) for ouput

		zend_hash_move_forward(sess->value.ht);
	}

//omitted stuff

}
Basically the problem is, when the php is run, I only see the "test2" part show up in the print_r output. Weird thing is, if I do a print_r on $data before I run the second function, I can see that both "test" and "test2" are added. You may be thinking that the second function is just not adding it, but for brevity purposes, I can assure you that it is. The problem lies in the internal hash pointer to my knowledge. However reseting this, as I did in the second function (I also tried in various locations in the first function) had no affect.

I have also tried this by replacing $data with $_SESSION so I have a persistant value in the array. If I don't "add_to_array" the "get_array_info" displays the correct information. The problem has something to do with iterating through the array once, and then trying to do so again. But that is why I thought the zend_hash_internal_pointer_reset() function would solve the problem. It didn't. :x

Any thoughts? Do I need to explain anything more clearly? :?
Post Reply