Understanding AMF0 and AS3 Array.

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.

<?
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 .= <a href="http://www.php.net/print_r">print_r</a>($data, true);
    return $str;
  }
}
?&gt

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.

<pre>
<ol>

    <li>

<div>import flash.net.Responder;</div></li>


    <li>

<div>var conn : RemotingConnection = new RemotingConnection( "http://localhost/amfphp/gateway.php");</div></li>


    <li>

<div>//</div></li>


    <li>

<div>var simpleArray=[9,8,7];</div></li>


    <li>

<div>conn.call( "AS3ArrayTest.convertToString", new Responder(onResult1,null), simpleArray);</div></li>


    <li>

<div>//</div></li>


    <li>

<div>var associativeArray=[1,2,3]</div></li>


    <li>

<div>associativeArray['key']='value';</div></li>


    <li>

<div>conn.call( "AS3ArrayTest.convertToString", new Responder(onResult2,null), associativeArray);</div></li>


    <li>

<div>//</div></li>


    <li>

<div>var object ={key:'value'}</div></li>


    <li>

<div>conn.call( "AS3ArrayTest.convertToString", new Responder(onResult3,null), object);</div></li>


    <li>

<div>//</div></li>


    <li>

<div>var objectWithArray ={key:'value', simpleArray:[4,5]}</div></li>


    <li>

<div>conn.call( "AS3ArrayTest.convertToString", new Responder(onResult4,null), objectWithArray);</div></li>


    <li>

<div>//</div></li>


    <li>

<div>function onResult1( result  ) : void{</div></li>


    <li>

<div>    t1_txt.text=result</div></li>


    <li>

<div>}</div></li>


    <li>

<div>//</div></li>


    <li>

<div>function onResult2( result  ) : void{</div></li>


    <li>

<div>    t2_txt.text=result</div></li>


    <li>

<div>}</div></li>


    <li>

<div>//</div></li>


    <li>

<div>function onResult3( result  ) : void{</div></li>


    <li>

<div>    t3_txt.text=result</div></li>


    <li>

<div>}</div></li>


    <li>

<div>//</div></li>


    <li>

<div>function onResult4( result  ) : void{</div></li>


    <li>

<div>    t4_txt.text=result</div></li>


    <li>

<div>}</div></li>

</ol>

</pre>

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

Leave a Reply