First, sorry for not submitting a *real* patch, i'm currently working on an older version but still thought this would be nice to submit.
PHPAPI.pas:
<!-- snip -->
type
tzvalVariantArray = array of variant;
function zval2variantarray(ht: PHashTable): tzvalVariantArray;
var
i : integer;
tmp : ^ppzval;
varr: tzvalVariantArray;
keystr : PChar;
keylen : uint;
idx : ulong;
begin
SetLength(varr, zend_hash_num_elements(ht));
zend_hash_internal_pointer_reset(ht);
for i := 0 to zend_hash_num_elements(ht)-1 do
begin
new(tmp);
try
if not (zend_hash_index_find(ht, i, tmp) = SUCCESS) then
begin
zend_hash_get_current_key_ex(ht, keystr, keylen, idx, false, nil);
zend_hash_get_current_data(ht, tmp);
end;
if (keylen = 0) then
varr[i] := zval2variant(tmp^^^)
else
varr[i] := VarArrayOf([StrPas(keystr), zval2variant(tmp^^^)]);
function zval2variant(Value: zval): variant;
begin
case Value._type of
IS_NULL: Result := NULL;
IS_LONG: Result := Value.Value.lval;
IS_DOUBLE: Result := Value.Value.dval;
IS_STRING: Result := zval2string(Value);
IS_BOOL: Result := boolean(Value.Value.lval);
IS_ARRAY: Result := zval2variantarray(Value.value.ht);
else
Result := NULL;
end;
end;
<!-- /snip -->
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks.
It's still a bit rough around the edges, like the way indexed (key=>value) arrays are handled, but it's a starting point... also, the 'tzvalVariantArray' type probably isn't necessary (using array of variant instead) but some juggling would be needed.
Keep up the excellent work!
Joao Inacio
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
First, sorry for not submitting a *real* patch, i'm currently working on an older version but still thought this would be nice to submit.
PHPAPI.pas:
<!-- snip -->
type
tzvalVariantArray = array of variant;
function zval2variantarray(ht: PHashTable): tzvalVariantArray;
var
i : integer;
tmp : ^ppzval;
varr: tzvalVariantArray;
keystr : PChar;
keylen : uint;
idx : ulong;
begin
SetLength(varr, zend_hash_num_elements(ht));
zend_hash_internal_pointer_reset(ht);
for i := 0 to zend_hash_num_elements(ht)-1 do
begin
new(tmp);
try
if not (zend_hash_index_find(ht, i, tmp) = SUCCESS) then
begin
zend_hash_get_current_key_ex(ht, keystr, keylen, idx, false, nil);
zend_hash_get_current_data(ht, tmp);
end;
if (keylen = 0) then
varr[i] := zval2variant(tmp^^^)
else
varr[i] := VarArrayOf([StrPas(keystr), zval2variant(tmp^^^)]);
zend_hash_move_forward_ex(ht, nil);
finally
freemem(tmp);
end;
end;
Result := varr;
end;
function zval2variant(Value: zval): variant;
begin
case Value._type of
IS_NULL: Result := NULL;
IS_LONG: Result := Value.Value.lval;
IS_DOUBLE: Result := Value.Value.dval;
IS_STRING: Result := zval2string(Value);
IS_BOOL: Result := boolean(Value.Value.lval);
IS_ARRAY: Result := zval2variantarray(Value.value.ht);
else
Result := NULL;
end;
end;
<!-- /snip -->
php code:
<?php
$something = array('test', 'key1' => 'value1', 'key2' => 2, 10, 20)
call_myfunc( $something );
?>
My function handler:
procedure TForm.MyFuncExecute(Sender: TObject;
Parameters: TFunctionParams; var ReturnValue: Variant; ThisPtr: Pzval;
TSRMLS_DC: Pointer);
var
i : integer;
arr : tzvalVariantArray;
key,
elem : variant;
begin
arr := Parameters.Values('paramarray');
for i := 0 to length(arr) -1 do
begin
elem := arr[i];
if VarIsArray(elem) then
begin
key := elem[0];
elem := elem[1];
end;
end;
end;
Thanks!
Very interesting solution, I will use it in the next release
Thanks.
It's still a bit rough around the edges, like the way indexed (key=>value) arrays are handled, but it's a starting point... also, the 'tzvalVariantArray' type probably isn't necessary (using array of variant instead) but some juggling would be needed.
Keep up the excellent work!
Joao Inacio