PHP Extension: Working with arrays

10 May

What if you are unable to work with arrays while building PHP extensions?

It will be like you have missed half of the picture, I guess.

Before discussing how you can handle and create arrays when building PHP Extensions, I will discuss what are arrays and how are they used in c.

Array is block of memory.

Some time it is said that an array is pointer, which is not true in all cases. Array can act as pointer and pointer as an array, however they are different.

To declare an array in c, simply write

int num[5];

This num now represent block of memory. The size id 5, so it take 20 bytes, keep in mind that an integer hold 4 bytes.

To assign value to first index of the array, write

*num = 10;

Although this can be done, however this is usually achieved by writing

num[0] = 10;

The above two statements are identical. However one is done using pointers and the other using array index.

To give value to second index, write

*(num+1) = 20;

or

num[1] = 20;

Let’s take our discussion a bit further.

To have an access to array address, we can simply do like this

int *p;

p = num;

First we declare an integer pointer, and then assign it an array. It means that the integer pointer p hold the memory address of the first element of array num.

This can also be achieved by writing

p = & num[0];

p now hold the address of the first element of the array.

Okay, while taking about an array and pointers earlier, I said they are a bit different although they can act as each other. Consider the following example;

double num[10];

double *dp = num;

If you want to access the fourth element of the array, write

dp[4];

This is similar to

num[4];

With pointer, however you can done this

dp++;

it means, move to the next memory address. However

num++;

can not be done, because num is an array not a pointer. This will not take you to the next memory address.

Well, when it come to passing an array as argument to a function, this can be achieved using the following methods in c.

long n[100];

f(n);

void f(long *lp)

{

}

In the first function we define it as taking the address of the array using pointer. Changing the values of the pointer location will modify the original array.

void f(long l[100])

{

}

In this example we take array as an array. This array is now local to the function, so changing it doesn’t cause the original array to be modified.

Although you can define the size of the array, however better approach is to leave it empty like

void f(long l[])

{

}

The compiler will keep track of the size of the array.

Now let’s have a look at how you can better use array while building extensions.

Array can easily be passed as parameters and returned from function.

Look at the following code which build and return an array.

PHP_FUNCTION(return_array)

{

zval *subarr;

init_array(return_value);

add_next_index_null(return_value);

add_next_index_long(return_value, 42);

add_next_index_string(return_value, “hello”,1);

add_next_index_double(return_value, 3.1415);

add_assoc_string(return_value, “name”,”faheem”,1);

add_index_long(return_value, 100, 33);

// to add sub array to the array

MAKE_STD_ZVAL(subarr);

init_array(subarr);

add_next_index_long(subarr, 45);

add_next_index_string(subarr, “sub array”,1);

add_next_index_zval(return_array, subarr);

}

While working with the array, you will first need to initialize an array as

init_array(array_name);

Once array is initialized, you can add values to it. Several methods are used for this purpose.

add_next_index_*() method is used to add value to next available method.

add_index_*() is used to add value to given index.

add_assoc_* is used to add value to key. This is used for creating an associated value.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: