问题
I have a client server scenario where the type conversion did by the SoapClient class in PHP, cannot tell wether an empty array is associative or numeric, and so defaults to numeric.
All exposed functions use basic types, no classes.
An associative array such as array("something"=>123)
gets converted to a map data type. However, when the same array is empty, such as array()
, it gets converted to an array on the Ruby side. Type casting to object (object)array()
will result in a struct data type on the Ruby side.
The argument is a bit more complex, not as simple as above:
array(
"options"=>array(
"plans"=>array(
0=>array(
"name"=>"abc",
"product_options"=>array(
"optional_key_determines_associative_array_data_type"=>0,
),
),
),
),
);
If the array under "product_options"
is empty, it gets converted to an array in Ruby, instead of a map. Once again, type casting to object in PHP results in a struct in Ruby.
What can I do on the PHP side to make empty "associative" arrays end up as maps on the Ruby side?
PHP 5.3.3, using SoapClient. Ruby 1.8.7, Rails 2.3.2 using Action Web Service.
回答1:
You can wrap your array in a SoapVar class with APACHE_MAP
as encoding parameter. Like This:
array(
"options"=>array(
"plans"=>array(
0=>array(
"name"=>"abc",
"product_options"=> new SoapVar(array(), APACHE_MAP),
),
),
),
);
回答2:
Well, this is exactly what I mean: To overcome that problem you will need to change the logic in your scripts - not the PHP. As you can not define a PHP array to be associative you will need to modify the receiving script.
If it was me I would not send an empty array. Put a status field into the array. This could be a field counting the available products which in this case would count 0. You will have a more meaningful communication AND the array IS suddenly associative no matter what e.g
"product_options" => array ('products'=>0,'...'=>...) and so forth.
What I say is you will need to change the logic, you can not change PHP.
Hope that helps,
Uwe
回答3:
I might be wrong here, but by my understanding:
I do believe what you are trying to achieve is not possible. An array (and we are talking array only, no objects) is an array. The structure given by the content makes an array associative or not.
An empty array is not associative.
来源:https://stackoverflow.com/questions/11013665/empty-associative-array-soap-type-conversion