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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<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