# transversing associative arrays from C

- Thread: https://externals.io/message/54953
- Original archive: https://news-web.php.net/php.internals/54953
- Started by Aaron Gray on 2011-08-25T21:57:07+00:00
- Messages: 10
- 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

---

## #54953 — Aaron Gray — 2011-08-25T21:57:07+00:00

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

Hi,

I am working on a C extension module for PHP and am wondering about an API
issue.

I can see how to add to an associative array :-

zval *aa;
MAKE_STD_ZVAL(aa);
array_init(aa);

Hi,

I am working on a C extension module for PHP and am wondering about an API
issue.

I can see how to add to an associative array :-

zval *aa;
MAKE_STD_ZVAL(aa);
array_init(aa);

add_assoc_string(aa, "test", "test value", 1);
add_assoc_string(aa, "test2", "test value2", 1);

but how do I transverse an associative array of values in a for loop simular
to using "foreach" in PHP ?

Hope this is not too OT for this list, but I could not find a better place
to ask the question.

I have looked through the API SVN sources and this seems like a possible
blind spot.

Many thanks in advance,

Aaron

but how do I transverse an associative array of values in a for loop simular
to using "foreach" in PHP ?

Hope this is not too OT for this list, but I could not find a better place
to ask the question.

I have looked through the API SVN sources and this seems like a possible
blind spot.

Many thanks in advance,

Aaron

---

## #54955 — Chris Stockton — 2011-08-25T23:55:05+00:00

URL: https://externals.io/message/54953#54955 · In reply to: #54953

Hello,


> Hi,
>
> I am working on a C extension module for PHP and am wondering about an API
> issue.
>
> I can see how to add to an associative array :-
>

Most everything you need to know about manipulating arrays can be
found in ext/standard/array.c

-Chris

---

## #54956 — Aaron Gray — 2011-08-26T00:16:09+00:00

URL: https://externals.io/message/54953#54956 · In reply to: #54955

"Chris Stockton" <chrisstocktonaz@gmail.com> wrote in message 
news:CALKFbxvsUq-zQyYtdXi3a9tcciEH6xzZ9Y-kByqVa8kpC4-NiQ@mail.gmail.com...
> Hello,
>
> 
> > […]
>
> Most everything you need to know about manipulating arrays can be
> found in ext/standard/array.c

Where's that in the SVN source tree ?

Aaron

---

## #54957 — Christopher Jones — 2011-08-26T00:18:42+00:00

URL: https://externals.io/message/54953#54957 · In reply to: #54956

> "Chris Stockton" <chrisstocktonaz@gmail.com> wrote in message news:CALKFbxvsUq-zQyYtdXi3a9tcciEH6xzZ9Y-kByqVa8kpC4-NiQ@mail.gmail.com...
> > […]
>
> Where's that in the SVN source tree ?
>
> Aaron

http://svn.php.net/viewvc/php/php-src/branches/PHP_5_4/ext/standard/

Chris

-- 
Email: christopher.jones@oracle.com
Tel:  +1 650 506 8630
Blog:  http://blogs.oracle.com/opal/

---

## #54958 — Aaron Gray — 2011-08-26T00:18:52+00:00

URL: https://externals.io/message/54953#54958 · In reply to: #54956

""Aaron Gray"" <ang.usenet@gmail.com> wrote in message 
news:A1.A0.22704.0D5E65E4@pb1.pair.com...
> "Chris Stockton" <chrisstocktonaz@gmail.com> wrote in message 
> news:CALKFbxvsUq-zQyYtdXi3a9tcciEH6xzZ9Y-kByqVa8kpC4-NiQ@mail.gmail.com...
> > […]
>
> Where's that in the SVN source tree ?

Ah, got it many thanks,

Aaron

---

## #54959 — Aaron Gray — 2011-08-26T00:52:59+00:00

URL: https://externals.io/message/54953#54959 · In reply to: #54957

"Christopher Jones" <christopher.jones@oracle.com> wrote in message 
news:4E56E662.90804@oracle.com...
>
>
> 
> > […]
>
> http://svn.php.net/viewvc/php/php-src/branches/PHP_5_4/ext/standard/

 Chris,

How much can I access as a PHP plugin dll or shared object ?

Thinking about it what I really want to do is see if a key exists in an 
array and get its type to see if it is BOOL and that it is TRUE.

Many thanks,

Aaron

---

## #54960 — Stas Malyshev — 2011-08-26T01:03:18+00:00

URL: https://externals.io/message/54953#54960 · In reply to: #54959

Hi!


> Thinking about it what I really want to do is see if a key exists in an
> array and get its type to see if it is BOOL and that it is TRUE.

For this, you will need to call zend_hash_find (or zend_symtable_find if 
there's a possibility of keys like "0" that have to be treated as 0). If 
you get SUCCESS, the key exists and you'll be getting zval **, then 
either check it's type and value with Z_TYPE_PP and Z_LVAL_PP or call 
zend_is_true(*val) to get generic truth value (like if(), etc. do).
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #54962 — Aaron Gray — 2011-08-26T03:19:17+00:00

URL: https://externals.io/message/54953#54962 · In reply to: #54960

Hi Stas,

"Stas Malyshev" <smalyshev@sugarcrm.com> wrote in message 
news:4E56F0D6.20608@sugarcrm.com...
> Hi!
>
> 
> > […]
>
> For this, you will need to call zend_hash_find (or zend_symtable_find if 
> there's a possibility of keys like "0" that have to be treated as 0).

Okay, found it, "zend_hash.h", so that should be accessable to my module :)

> If you get SUCCESS, the key exists and you'll be getting zval **, then 
> either check it's type and value with Z_TYPE_PP and Z_LVAL_PP or call 
> zend_is_true(*val) to get generic truth value (like if(), etc. do).

Great.

That seems alot simpler.

Many thanks,

Aaron

---

## #54963 — Xinchen Hui — 2011-08-26T04:24:13+00:00

URL: https://externals.io/message/54953#54963 · In reply to: #54962

hi
  every api has lots of 'samples' ,that are the standard extensions in src/ext/*

   :)

Thanks
Sent from my iPhone

在 2011-8-26，11:20，Aaron Gray <ang.usenet@gmail.com> 写道：

> Hi Stas,
>
> "Stas Malyshev" <smalyshev@sugarcrm.com> wrote in message news:4E56F0D6.20608@sugarcrm.com...
> > […]
>
> Okay, found it, "zend_hash.h", so that should be accessable to my module :)
>
> > […]
>
> Great.
>
> That seems alot simpler.
>
> Many thanks,
>
> Aaron

---

## #54964 — Aaron Gray — 2011-08-26T04:48:47+00:00

URL: https://externals.io/message/54953#54964 · In reply to: #54963

Thanks !

"Xinchen Hui" <laruence@gmail.com> wrote in message 
news:7256329410255948429@unknownmsgid...
hi
  every api has lots of 'samples' ,that are the standard extensions in 
src/ext/*

   :)

Thanks
Sent from my iPhone

在 2011-8-26，11:20，Aaron Gray <ang.usenet@gmail.com> 写道：

> Hi Stas,
>
> "Stas Malyshev" <smalyshev@sugarcrm.com> wrote in message 
> news:4E56F0D6.20608@sugarcrm.com...
> > […]
>
> Okay, found it, "zend_hash.h", so that should be accessable to my module 
> :)
>
> > […]
>
> Great.
>
> That seems alot simpler.
>
> Many thanks,
>
> Aaron
