next up previous contents
Next: Navigating PHP Arrays Up: Usage Previous: Passing Arguments   Contents

Creating PHP Arrays

As long as the program has included either php_cxx.h, php_stl.h, or php_arr.h, a php_array object can be instantiated as follows:

php_array a;
php_array b(a);

The second php_array object b is created as a deep copy of the first object a although it is a bit pointless in this example as a is still empty.

There are three ways to add elements to a php_array in C++ (although just as with PHP, clients aren't limited to using any single method for any php_array object):

Inserting data into the php_array uses the same mechanism as passing arguments to PHP functions. Refer to Table 1 on page [*] for the list of argument specifiers.

a.add("l", 5);
a.add_assoc("slsd", "one", 1, "two", 2.5);
a.add_index("lsll", 6, "six", 128, 129);

It is important to observe that when adding associative elements each even argument (starting with the 0th element) must be a string. Similarly, when adding indexed elements each even argument (again starting with the 0th element) must be a long.

Data can also be removed from the array by associative or numerical index

a.remove(6);
a.remove("two");

Passing these arrays into PHP as the argument to a function then is as simple as using the php_array argument specifier and passing a reference to the object.

p.call_void("print_r", "a", &a);

This particular call, in the context of the other code above, produces the following operational output:

My Output: Array
(
    [0] => 5
    [one] => 1
    [128] => 129
)


next up previous contents
Next: Navigating PHP Arrays Up: Usage Previous: Passing Arguments   Contents
Andrew Bosworth 2008-03-24