【问题标题】:How to Get Array From URL Encoded String?如何从 URL 编码字符串中获取数组?
【发布时间】:2013-05-08 13:45:03
【问题描述】:

我确信这是一件非常容易的事情,但我在谷歌上几个小时都找不到它。 我是 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.
  };
}

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

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

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

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

【问题讨论】:

  • 我的回复对您有帮助吗?

标签: php arrays actionscript-3 urlvariables


【解决方案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]);
}

【讨论】:

    【解决方案2】:

    我认为您正在寻找 parse_str 函数

    parse_str($str, $output);
    

    【讨论】:

    • 谢谢,但我的问题出在 ActionScript 方面。我正在尝试从 PHP 正确生成的字符串中解析变量。
    【解决方案3】:

    对不起,我以为这是一个 PHP 问题。在 ActionScript 中,试试这个:

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

    【讨论】:

    • 谢谢,但我的问题出在 ActionScript 方面。我正在尝试从 PHP 正确生成的字符串中解析变量。
    • 进行了编辑,立即尝试。不过,您对 URLVariables 的看法是正确的。
    猜你喜欢
    • 2011-05-15
    • 2011-07-20
    • 2011-05-08
    • 2021-09-22
    • 1970-01-01
    • 2012-07-13
    • 2011-11-17
    • 2011-05-31
    • 2015-03-26
    相关资源
    最近更新 更多