# What's wrong with these hashtable iteration code?

- Thread: https://externals.io/message/75733
- Original archive: https://news-web.php.net/php.internals/75733
- Started by Aaron Lewis on 2014-07-21T03:12:19+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #75733 — Aaron Lewis — 2014-07-21T03:12:19+00:00

URL: https://externals.io/message/75733#75733

Hi,

I'm trying to iterate through a hash table,

But the zend_hash_get_current_key() doesn't seem to move forward:
I'm getting duplicate output at the 'fprintf' part.

       for(zend_hash_internal_pointer_reset_ex(ht, &pos);
                zend_hash_has_more_elements_ex(ht, &pos) == SUCCESS;
                zend_hash_move_forward_ex(ht, &pos))
        {
            if (zend_hash_get_current_key (ht, &string_key, &num_key, 0)
                    != HASH_KEY_IS_STRING)
                continue;

            fprintf (stderr, "string key: %s\n", string_key);
        }

Any ideas?


-- 
Best Regards,
Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/
Finger Print:   9F67 391B B770 8FF6 99DC  D92D 87F6 2602 1371 4D33

---

## #75734 — Tjerk Meesters — 2014-07-21T03:20:21+00:00

URL: https://externals.io/message/75733#75734 · In reply to: #75733

On Mon, Jul 21, 2014 at 11:12 AM, Aaron Lewis <the.warl0ck.1989@gmail.com>
wrote:

> Hi,
>
> I'm trying to iterate through a hash table,
>
> But the zend_hash_get_current_key() doesn't seem to move forward:
> I'm getting duplicate output at the 'fprintf' part.
>
>        for(zend_hash_internal_pointer_reset_ex(ht, &pos);
>                 zend_hash_has_more_elements_ex(ht, &pos) == SUCCESS;
>                 zend_hash_move_forward_ex(ht, &pos))
>         {
>             if (zend_hash_get_current_key (ht, &string_key, &num_key, 0)
>                     != HASH_KEY_IS_STRING)
>                 continue;
>
>             fprintf (stderr, "string key: %s\n", string_key);
>         }
>

The _ex() hash iteration functions allow for iterating a hash without
modifying its internal pointer, but zend_hash_get_current_key() uses only
the internal pointer.

The function you need to use is: zend_hash_get_current_key_ex() and pass
&pos to it.



>
> Any ideas?
>
>
> --
> Best Regards,
> Aaron Lewis - PGP: 0x13714D33 - http://pgp.mit.edu/
> Finger Print:   9F67 391B B770 8FF6 99DC  D92D 87F6 2602 1371 4D33
>
> --
> 
> 
>
>


-- 
--
Tjerk
