0

我确信这是一件非常容易的事情,但我在谷歌上几个小时都找不到它。我是 ActionScript 的新手,我正在尝试从 .php 文件生成的字符串中获取变量数组。

我的 php 文件输出这个:

var1=42&var2=6&var3=string

我的 ActionScript 代码是:

public function CallAjax_VARIABLES(url:String , the_array:Array) 
{ 
 var request:URLRequest = new URLRequest(url); 
 var variables:URLLoader = new URLLoader(); 
 variables.dataFormat = URLLoaderDataFormat.VARIABLES; 
 variables.addEventListener(Event.COMPLETE, VARIABLES_Complete_Handler(the_array)); 
 try 
 { 
  variables.load(request); 
 }  
 catch (error:Error) 
 { 
  trace("Unable to load URL: " + error); 
 } 
} 

function VARIABLES_Complete_Handler(the_array:Array):Function {
  return function(event:Event):void {
  var loader:URLLoader = URLLoader(event.target); 
  //the_array = loader.data;   // this doesn't work.  
  //the_array = URLVariables.decode(loader); // this doesn't work either.
  //trace(loader.data['var1']); // this outputs 42, so I'm getting the string from php.
  };
}

我想你已经理解了这一点,但最后,我想要一个数组(在 ActionScript中),它会给我:

the_array['var1']=42;
the_array['var2']=6;
the_array['var3']="string";

我究竟做错了什么?我应该怎么办?谢谢!

编辑:我正在尝试从 php 到 ActionScript 获取变量。例如,我的 PHP 文件正确地将数组转换为 html 查询,但我不知道如何在 ActionScript 中将它们解析为数组。

4

3 回答 3

1

你应该使用URLVariables这个。

var vars:URLVariables = new URLVariables(e.target.data);

这样你就可以简单地说:

trace(vars.var2); // 6

数组在这里没有用,因为结果是关联的而不是基于索引的,尽管您可以轻松地获取所有值并通过简单的循环将它们放入数组中:

var array:Array = [];
for(var i:String in vars)
{
    array.push(vars[i]);
}
于 2013-05-09T05:06:38.240 回答
0

抱歉,我以为这是一个 PHP 问题。在 ActionScript 中,试试这个:

 var the_array:URLVariables = new URLVariables();
 the_array.decode(loader.data);

 trace(the_array.var1);
于 2013-05-08T13:47:08.730 回答
0

我认为您正在寻找 parse_str 函数

parse_str($str, $output);
于 2013-05-08T13:48:00.913 回答