You might have already read about Array oddity in AMFPHP/Flex 2. AS3 Associative Arrays, when sent through AMF0 they include ‘length’ propery. But numeric index based Arrays are fine.

I’ve created the following AMFPHP Service to showcase this issue. It is a simple service with a remote method that converts any given object to string and returns it.

[cc lang=”actionscript3″] <? class AS3ArrayTest{ function AS3ArrayTest(){ @include_once(“AS3ArrayTest.methodTable.php”); } /**Converts the given data to String and returns it *@param Data Any, Data that needs to be converted *@access remote *@returns String */ function convertToString($data){ $str = “PHP dataType: “.gettype($data).”n”; $str .= print_r($data, true); return $str; } } ?&gt [/cc]

Then I’ve created a Flash 9 AS3 Fla to send different combinations of data to this service. here is the script that does it all.

[cc lang=”actionscript3″]

  1. import flash.net.Responder;
  2. var conn : RemotingConnection = new RemotingConnection( "http://localhost/amfphp/gateway.php");
  3. //
  4. var simpleArray=[9,8,7];
  5. conn.call( "AS3ArrayTest.convertToString", new Responder(onResult1,null), simpleArray);
  6. //
  7. var associativeArray=[1,2,3]
  8. associativeArray['key']='value';
  9. conn.call( "AS3ArrayTest.convertToString", new Responder(onResult2,null), associativeArray);
  10. //
  11. var object ={key:'value'}
  12. conn.call( "AS3ArrayTest.convertToString", new Responder(onResult3,null), object);
  13. //
  14. var objectWithArray ={key:'value', simpleArray:[4,5]}
  15. conn.call( "AS3ArrayTest.convertToString", new Responder(onResult4,null), objectWithArray);
  16. //
  17. function onResult1( result ) : void{
  18. t1_txt.text=result
  19. }
  20. //
  21. function onResult2( result ) : void{
  22. t2_txt.text=result
  23. }
  24. //
  25. function onResult3( result ) : void{
  26. t3_txt.text=result
  27. }
  28. //
  29. function onResult4( result ) : void{
  30. t4_txt.text=result
  31. }

[/cc]


Here is the result.

Now we can clearly see what is happening. so here is what I suggest

  • If you need to send index based Array, no problem use Array
  • If you need to send key value pairs, use Object instead
  • If you can ignore the ‘length’ property on the server side you may still use Associative Array
  • Else you may keep the simple Array as one of the elements of your Object and send the Object