I'm starting to write a PHP extension and wish to just get my head around how to loop through an array that is passed (with the intention of changing the data value by value). Preferred method would be a for loop so that I can match array1 with array2 data e.g. array1[0] is linked to array2[0], [1] with [1] etc...
Anyone able to help?
modarray.c
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
extern zend_module_entry modarray_module_entry;
#define phpext_modarray_ptr &modarray_module_entry
PHP_FUNCTION(modarray);
static function_entry modarray_functions[] = {
PHP_FE(modarray, NULL)
PHP_FE_END
};
zend_module_entry modarray_module_entry = {
STANDARD_MODULE_HEADER,
"modarray",
modarray_functions,
NULL,
NULL,
NULL,
NULL,
NULL,
"0.1",
STANDARD_MODULE_PROPERTIES
};
ZEND_GET_MODULE(modarray)
PHP_FUNCTION(modarray)
{
zval *val, *val2;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z", &val, &val2) == FAILURE){
return;
}
SEPARATE_ZVAL(&val);
SEPARATE_ZVAL(&val2);
array_init(return_value);
zval_add_ref(&val);
zval_add_ref(&val2);
add_next_index_zval(return_value, val);
add_next_index_zval(return_value, val2);
}
PHP Code
<?php
$array1 = array(1,2,3,4);
$array2 = array(5,6,7,8);
echo '<pre>';
print_r(modarray($array1,$array2));
echo '</pre>';
?>
PHP Output
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
[1] => Array
(
[0] => 5
[1] => 6
[2] => 7
[3] => 8
)
)